1 / 13

GUI Programming and Events GUI = Graphical User Interface

GUI Programming and Events GUI = Graphical User Interface. GUI components page 2 An excerpt from the class tree page 3 Pushing a button page 4-5 A little from the API (Container, JButton, AbstractButton) page 6 More about the pushbutton example page 7 Inner classes page 8

Download Presentation

GUI Programming and Events 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 Programming and EventsGUI = Graphical User Interface GUI components page 2 An excerpt from the class tree page 3 Pushing a button page 4-5 A little from the API (Container, JButton, AbstractButton) page 6 More about the pushbutton example page 7 Inner classes page 8 Textfields page 9 Handling the layout page 10-13 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  2. GUI Components in Swing Demo program at jdk1.4/demo/jfc/SwingSet2/SwingSet2.html Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  3. An Excerpt from the Class Tree Object Component Container Panel Applet JApplet JComponent Color Font JComboBox JTextComponent JPanel JLabel JList JTextField JTextArea JMenuBar Remember that the class names start with an J! AbstractButton JPane JPopupMenu JMenuItem JButton JToggleButton JScrollbar JScrollPane JCheckBox JRadioButton JTable JTree JRadioButtonMenuItem JCheckBoxMenuItem JMenu Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  4. Pushing a Button One line to the console for every button push: • An event occurs every time you push the button. • A user may fire many types of events in a GUI program. • An event may result in some sort of reaction from the program. • We may picture an ”endless loop” in the program. This loop contains an enormous switch statement. • Consider that this loop is running inside the Java interpreter, and we as programmers only deal with the contents of each individual case block. You pushed the button! You pushed the button! You pushed the button! while (true) { switch (event) { case passwordInput check password break; case print print the document break; case the button "Calculate Tax" is pushed calculate tax break; case ... ... } } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  5. The Source Code and the Messages Sent public class PushbuttonApplet extends JApplet { public void init() { Container guiContainer = getContentPane(); LayoutManager layout = new FlowLayout(); guiContainer.setLayout(layout); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); ButtonListener theButtonListener = new ButtonListener(); myButton.addActionListener(theButtonListener); } } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("You pushed the button!"); } } 1. Create the button object. 2. Put the button into the GUI container. 3. Create the listener object. 4. Tell the listener to react to events thatoccur with the button.Every kind of listener requires us to implement a certain interface. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  6. A Little from the Java API • The java.awt.Container class • public void setLayout(LayoutManager layoutManager) • public Component add(Component comp) • The javax.swing.Jbutton class • public JButton() • public JButton(String text) • public JButton(Icon icon) • public JButton(String text, Icon icon) • The javax.swing.AbstractButton class • public void addActionListener(ActionListener aListener) • public void setMnemonic(char mnemonic) • public void setEnabled(boolean open) • public void setText(String text) Solve all the problems, page 394. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  7. public class ColorButtonApplet extends JApplet { public void init() { Container guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); ButtonListener theButtonListener = new ButtonListener(guiContainer); myButton.addActionListener(theButtonListener); } } class ButtonListener implements ActionListener { private Container theGuiContainer; public ButtonListener(Container initContainer) { theGuiContainer = initContainer; } public void actionPerformed(ActionEvent event) { theGuiContainer.setBackground(Color.red); } } public class ColorButtonApplet2 extends JApplet { private Container guiContainer; public void init() { guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); ButtonListener theButtonListener = new ButtonListener(); myButton.addActionListener(theButtonListener); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { guiContainer.setBackground(Color.red); } } } More on the Pusbutton Example What if we want a push to result in a red background color? An inner class is more practical: Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  8. Inner Classes • An inner class is a member of the outer class. • An instance of an inner class always belongs to a certain instance of the outer class. • The accessibilty of the inner class may be controlled by an access modifier. • In this book, almost every inner class is private. • In this book, we use inner classes in connection with GUI programming • We create listener classes. • We create complex GUIs by assembling smaller parts. Every part (”panel”) will be an instance of an inner class. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  9. Text Fields • The javax.swing.Jlabel class • public JLabel() • public JLabel(String text) • public JLabel(String text, int horizontalAdjusting) • public void setText(String text) • The javax.swing.JTextField class • public JTextField() • public JTextField(String text) • public JTextField(int noOfColumns) • public JTextField(String text, int noOfColumns) • public void setHorizontalAlignment( int horizontalAdjusting) • public int getHorizontalAlignment() • public void setFont(Font newFont) • public void addActionListener( ActionListener aListener) • The javax.swing.text.JTextComponent class • public String getText() • public void setText(String text) • public void setEditable(boolean open) public class NameApplet extends JApplet { private Container guiContainer; private JTextField nameField = new JTextField(20); private JLabel greeting = new JLabel(); public void init() { guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("What's your name?"); guiContainer.add(nameLabel); guiContainer.add(nameField); JButton myButton = new JButton("Push here!"); guiContainer.add(myButton); ButtonListener theButtonListener = new ButtonListener(); myButton.addActionListener(theButtonListener); guiContainer.add(greeting); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String name = nameField.getText(); greeting.setText("Hallo, " + name + "!"); } } } Solve all problems, page 401. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  10. Managing the Layout • The position of the components may be set by giving the fixed pixel positions. • However, using layout managers is the most usual way to lay out the components. • Up to now, FlowLayout is used: The components are layed out from left to right, centered on the lines: Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  11. BorderLayout -Default for Applets public class TestBorderLayout extends JApplet { public void init() { Container guiContainer = getContentPane(); Font big = new Font("SansSerif", Font.BOLD, 20); JButton buttonOne = new JButton("1"); buttonOne.setFont(big); guiContainer.add(buttonOne, BorderLayout.WEST); JButton buttonTwo = new JButton("2"); buttonTwo.setFont(big); guiContainer.add(buttonTwo, BorderLayout.CENTER); JButton buttonThree = new JButton("3"); buttonThree.setFont(big); guiContainer.add(buttonThree, BorderLayout.EAST); JButton buttonFour = new JButton("4"); buttonFour.setFont(big); guiContainer.add(buttonFour, BorderLayout.NORTH); JButton buttonFive = new JButton("5"); buttonFive.setFont(big); guiContainer.add(buttonFive, BorderLayout.SOUTH); } } 1 - WEST2 - CENTER 3 - EAST 4 - NORTH 5 - SOUTH 1 - WEST2 - CENTER 3 - EAST 1 - NORTH2 - CENTER 3 - SOUTH Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  12. GridLayout public class TestGridLayout extends JApplet { private JTextField name = new JTextField(15); private JTextField address = new JTextField(15); private JTextField phone = new JTextField(15); private JTextField eMail = new JTextField(15); public void init() { Container guiContainer = getContentPane(); guiContainer.setLayout(new GridLayout(4, 2, 5, 5)); JLabel label = new JLabel("Name:", JLabel.RIGHT); guiContainer.add(label); guiContainer.add(name); label = new JLabel("Address:", JLabel.RIGHT); guiContainer.add(label); guiContainer.add(address); label = new JLabel("Phone:", JLabel.RIGHT); guiContainer.add(label); guiContainer.add(phone); label = new JLabel("E-mail:", JLabel.RIGHT); guiContainer.add(label); guiContainer.add(eMail); } } The GridLayout constructor takes the following arguments: no. of rows, no. of columns, horizontal and vertical gap between the cells. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  13. Dividing a GUI Container in Several Small Containers (Panels) northern part, GridLayout middle part, FlowLayout southern part, FlowLayout Show program listing 13.8 pp. 408-411. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

More Related