1 / 19

GUI: Graphical User Interface

GUI: Graphical User Interface. AWT/SWING: Components Drag and Drop in NetBeans Events Listeners. Components. GUIs are build using standard components, for instance: Windows (Frames in Java) Menu bars Text fields List boxes Buttons Etc. Java.awt and javax.swing. java.lang.Object.

Download Presentation

GUI: Graphical User Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners IntroJava2006 AAU

  2. Components • GUIs are build using standard components, for instance: • Windows (Frames in Java) • Menu bars • Text fields • List boxes • Buttons • Etc. IntroJava2006 AAU

  3. Java.awt and javax.swing IntroJava2006 AAU

  4. java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent java.awt.Window javax.swing.JPanel java.awt.Frame javax.swing.JFileChooser javax.swing.JPopupMenu javax.swing.JFrame javax.swing.JToolbar javax.swing.JLabel Some AWT and Swing Classes Note the use of inheritance IntroJava2006 AAU

  5. Graphical User Interfaces • A Graphical User Interface (GUI) is created with at least three kinds of objects • components • events • Listeners • Java standard class library provides these objects for us IntroJava2006 AAU

  6. Components and Containers • A GUI component defines a screen element to display information or allow the user to interact with the program • buttons, text fields, labels, menus, etc. • A container is a special component that holds and organizes other components • dialog boxes, applets, frames, panels, etc. IntroJava2006 AAU

  7. Hello World Example import javax.swing.*; public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200,200); frame.setVisible(true); } } IntroJava2006 AAU

  8. Frame • The main component is a frame. • Frame is the Java term for what is generally called a ‘window’. • In Swing frames are realised by the class JFrame • A frame is a rectangular area: • title bar at the top • The title bar contains buttons for closing, maximizing and minimizing of the frame • Below the title bare is an area at which further graphical components can be placed • This area is divided into two parts: • space for a menu bar at the upper edge • content pane below IntroJava2006 AAU

  9. Elements of a JFrame Window controls Title Menu bar Content pane IntroJava2006 AAU

  10. Import libraries import java.awt.*; import javax.swing.*; public class FirstFrame extends JFrame { public FirstFrame() { super("My First Frame"); setSize(200,200); setVisible(true); } } Inherit JFrame IntroJava2006 AAU

  11. import java.awt.*; import javax.swing.*; public class SecondFrame extends JFrame{ //Components neede to build the menubar private JMenuBar menuBar; private JMenu fileMenu, editMenu; private JMenuItem openItem, quitItem; public SecondFrame() { super("My Second Frame"); buildMenu(); setSize(200,200); setVisible(true); } private void buildMenu(){ menuBar = new JMenuBar(); fileMenu = new JMenu("File"); editMenu = new JMenu("Edit"); menuBar.add(fileMenu); menuBar.add(editMenu); openItem = new JMenuItem("Open"); quitItem = new JMenuItem("Quit"); fileMenu.add(openItem); fileMenu.add(quitItem); this.setJMenuBar(menuBar); } IntroJava2006 AAU

  12. private void buildInputpart(){ panelIO = new JPanel(); panelIO.setLayout(null); labFirstName = new JLabel("First Name:"); labLastName = new JLabel("Last Name: "); txtFirstName = new JTextField(); txtFirstName.setToolTipText("Input first name"); txtLastName = new JTextField(); txtLastName.setToolTipText("Input last name"); addComponent(panelIO, labFirstName, 20,20,70,20); addComponent(panelIO, txtFirstName, 100,20,150,20); addComponent(panelIO, labLastName, 20,45,70,20); addComponent(panelIO, txtLastName, 100,45,150,20); this.add(panelIO); } private void buildButtonpart(){ panelButton = new JPanel(); panelButton.setLayout(null); submitBut = new JButton("Submit"); clearBut = new JButton("Clear"); addComponent(panelButton, submitBut, 20,80, 100,20); addComponent(panelButton, clearBut, 130,80, 100,20); this.add(panelButton); } private void addComponent(Container container, Component c,int x,int y,int width,int height){ c.setBounds(x,y,width, height); container.add(c); } IntroJava2006 AAU

  13. This is unbearable! • Instead an IDE supporting Drag and Drop • For instance NetBeans: GUI Building in NetBeans IDE 5.0 or http://www.netbeans.org/kb/55/quickstart-gui.html IntroJava2006 AAU

  14. Creating a GUI Project • Choose File > New Project. Alternately, you can click the New Project icon in the IDE toolbar. • In the Categories pane, select the General node and in the Projects pane, choose Java Application. Click Next. • Enter ContactEditor in the Project Name field and specify the project location. • Ensure that the Set as Main Project checkbox is selected and deselect Create Main Class if it is selected. • Click Finish. The IDE creates the ContactEditor folder on your system in the designated location. This folder contains all of the project's associated files, including its Ant script, folders for storing sources and tests, and a folder for project-specific metadata. To view the project structure, use the IDE's Files window. IntroJava2006 AAU

  15. Adding a Main Component To create a JFrame container: • In the Projects window, right-click the ContactEditor node and choose New > JFrame Form. • Enter ContactEditorUI as the Class Name. • Enter my.contacteditor as the package. • Click Finish. The IDE creates the ContactEditorUI form and the ContactEditorUI class within the ContactEditorUI.java application and opens the ContactEditorUI form in the GUI Builder. Notice that the my.contacteditor package replaces the default package. IntroJava2006 AAU

  16. Event Driven • Programs with graphical user interfaces (GUIs) are event driven: • the program reacts to actions of the user • these actions generate (‘fire’) events • examples of events are: • pressing a key • moving the mouse • clicking on a button • selecting a menu item • Components that reacts to events are called Listeners IntroJava2006 AAU

  17. Eventhandling Listener React to the events User interface GUI Components fires the event in response to being clicked Java code Components IntroJava2006 AAU

  18. Hello World – now with buttons! private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText("Hello World!"); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } Called when the event occurs We write the body of the event handler jButton1.setText("Say Hello"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); Listener generated by NetBeans IntroJava2006 AAU

  19. Do it!The tutorial: Adding Functionality to Buttons: A Beginners Guide IntroJava2006 AAU

More Related