Download presentation
Presentation is loading. Please wait.
Published byMary Edwards Modified over 9 years ago
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 14 Texts, Choices and Windows Focus listenerspage 2 Text componentspage 3-6 Giving the user the coice between alternativespage 7-9 Choices using check boxespage 10 Choices using radio buttons page 11 Choices using listspage 12-13 Java Windowspage 14 The class tree with the windows classespage 15 Creating a windowpage 16-18 The differences between applets and applicationspage 19-20 Other ways programming listenerspage 21-22
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 14, page 2 Focus Listeners A focus event occurs when a component gains or loses focus. Useful if, for example, we want to control input immediately after the user leaves an input field. If input is not ok, the focus may be set back to the field again (requestFocus()). A class describing focus listeners must implement the java.event.FocusListener interface: –public void focusGained() –public void focusLost() Often one of the methods is empty.
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 14, page 3 Text Components The javax.swing.text.JTextComponent class –public String getText() –public void setText(String text) –public void setEditable(boolean open) 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.JPasswordField class –public JPasswordField() –public JPasswordField(int noOfColumns) –public char[] getText() –public String getText() (deprecated! ) Object Component JTextComponent JTextAreaJTextField Container JComponent JPasswordField
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 14, page 4 An Example
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 14, page 5 Message Exchange
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 14, page 6 Show program listing 14.1, pp. 418-420 Solve the problem, page 423.
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 14, page 7 Giving the User a Choice Between Alternatives check boxes radio buttons list
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 14, page 8 GUI Componenter for Choices GUI componentClassListener will implement the interface Check box javax.swing.JCheckBoxjava.awt.ActionListener Radio button javax.swing.JRadioButton, usually placed in a javax.swing.ButtonGroup java.awt.ActionListener List javax.swing.JList, usually placed in a javax.swing.JScrollPane javax.swing.event. ListSelectionListener
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 14, page 9 GUI Components for Choices Object Component Container JComponent JToggleButton AbstractButton JRadioButton JList ButtonGroup JCheckBox
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 14, page 10 Choices Using Check Boxes Show program listing 14.2 pp. 426-427. Solve the problems, page 428. Will have dinner. Will have lunch. Output after click on the dinner box Output after click on the lunch box The user first clicks on the dinner check box, then on the lunch check box. An ActionEvent is generated for every click. The console:
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 14, page 11 Choices Using Radio Buttons Solve all problems, page 431. Show program listing 14.3 page 429-430.
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 14, page 12 Choices Using Lists The list can be set up such that the user can select only one item, an interval of items, or several intervals of items. List with fixed contents. Show program listing 14.4 page 432-433. Solve problem 1, page 439.
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 14, page 13 Choices Using Lists The changes have to be done in the list’s ”model”. ”The model” represents the data that will be displayed. ”The model” is described in the DefaultListModel class. The contents of the list displayed are described in the JList class. List with dynamic contents. Show program listing 14.5, p. 436-437. Solve problem 2, page 439. Clicking on the line with “Evy Wilde” gives:
14
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 14, page 14 Java Windows
15
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 14, page 15 The Class Tree for the Windows Classes Object Component JFrameJApplet JInternalFrameDialog JDialog Container WindowJComponentPanel FrameJWindow Applet
16
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 14, page 16 Creating a Windows import javax.swing.JFrame; class TestJFrame { public static void main(String[] args) { JFrame myFirstWindow = new JFrame("My First Window"); myFirstWindow.setVisible(true); myFirstWindow.setSize(300, 200); // the size in pixels } } // this program does not work correctly The window is closed, but the Java interpreter keeps running… The Java interpreter may be stopped by using System.exit(0);, but where should this statement be inserted? We have to create an object, listening to the window closing event.
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 14, page 17 The WindowListener Interface and the WindowAdapter Class The java.awt.event.WindowListener interface: void windowActivated(WindowEvent event); void windowClosed(WindowEvent event); void windowClosing(WindowEvent event); void windowDeactivated(WindowEvent event); void windowDeiconified(WindowEvent event); void windowIconified(WindowEvent event); void windowOpened(WindowEvent event); Often all these methods, except for one, have empty bodies, therefore there exists an adapter class (this is true for all awt listener interfaces with more than one method): package java.awt.event; public abstract class WindowAdapter implements WindowListener { public void windowOpened(WindowEvent event) {} public void windowClosing(WindowEvent event) {} //... and so on, only empty bodies }
18
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 14, page 18 An Example Using the Adapter Class Solve problems 2 and 3, page 446. import java.awt.*; import javax.swing.*; import java.awt.event.*; class JFrame2 extends JFrame { public JFrame2() { Container guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); guiContainer.add(new JLabel("This window handles closing. Close the window!")); addWindowListener(new MyWindowListener()); } private class MyWindowListener extends WindowAdapter { public void windowClosing(WindowEvent event) { System.out.println("Here we may write to data files, etc."); System.exit(0); // Remember this! } class TestJFrame2 { public static void main(String[] args) { JFrame mySecondWindow = new JFrame2(); mySecondWindow.setTitle("My Second Window"); mySecondWindow.setSize(400, 100); mySecondWindow.setVisible(true); }
19
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 14, page 19 Differences Between Applets and Applications The applet –The applet is a subclass of JApplet. –The applet class must have public access. –The user interface is constructed in the init() method. –The browser instantiates the applet object and sends the init() message to it. The application –The application is a subclass of JFrame. –Most of our applications have package access. –The user interface is constructed in the constructor. –The instantiation of application objects is coded in our programs, for example in the main() method.
20
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 14, page 20 Differences Between Applets and Applications, an Example 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); } class ColorButtonApplication extends JFrame { private Container guiContainer; public ColorButtonApplication(String title) { super(title); 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); } class TestColorButtonApplication { public static void main(String[] args) { ColorButtonApplication myWindow = new ColorButtonApplication("My Window"); myWindow.pack(); myWindow.setVisible(true); myWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }
21
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 14, page 21 Other Ways to Program Listeners: 1. The Window as Listener public class ColorButtonAppletThis extends JApplet implements ActionListener { private Container guiContainer; public void init() { guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); myButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { guiContainer.setBackground(Color.red); } The class describing the applet, is the same as the class implementing the ActionListener interface this is registered as a listener object The inner class ButtonListener is gone.
22
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 14, page 22 Other Ways to Program Listeners: 2. An Anonymous Class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.