Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Similar presentations


Presentation on theme: "Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and."— Presentation transcript:

1 Java the UML Way http://www.tisip.no/JavaTheUmlWay/ versjon 2002-04-17 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13 GUI Programming and Events GUI = Graphical User Interface GUI componentspage 2 An excerpt from the class treepage 3 Pushing a buttonpage 4-5 A little from the API (Container, JButton, AbstractButton) page 6 More about the pushbutton examplepage 7 Inner classespage 8 Textfieldspage 9 Handling the layoutpage 10-13

2 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 2 GUI Components in Swing Demo program at jdk1.4/demo/jfc/SwingSet2/SwingSet2.html

3 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 3 An Excerpt from the Class Tree JComponent JTextComponent AbstractButton JComboBox JLabel JList JMenuBar JPane JPopupMenu JScrollbar JScrollPane JTable JTree JToggleButton JButton JMenuItem JCheckBoxJRadioButton JRadioButtonMenuItemJCheckBoxMenuItem ContainerComponentObject JPanel JApplet AppletPanel JMenu FontColor JTextAreaJTextField Remember that the class names start with an J!

4 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 4 Pushing a Button 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. 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...... } One line to the console for every button push: You pushed the button!

5 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 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 that occur with the button. Every kind of listener requires us to implement a certain interface.

6 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 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.

7 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 7 More on the Pusbutton Example 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); } What if we want a push to result in a red background color? An inner class is more practical:

8 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 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.

9 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 9 Text Fields 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 + "!"); } 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) Solve all problems, page 401.

10 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 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:

11 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 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 - WEST 2 - CENTER 3 - EAST 4 - NORTH 5 - SOUTH 1 - WEST 2 - CENTER 3 - EAST 1 - NORTH 2 - CENTER 3 - SOUTH

12 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 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.

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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 13, page 13 Dividing a GUI Container in Several Small Containers (Panels) Show program listing 13.8 pp. 408-411. northern part, GridLayout middle part, FlowLayout southern part, FlowLayout


Download ppt "Java the UML Way versjon 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and."

Similar presentations


Ads by Google