Download presentation
Presentation is loading. Please wait.
Published byShanon McLaughlin Modified over 9 years ago
1
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
2
One listener for many buttons In last examples, we use one listener for one button. So when the corresponding actionPerformed method is invoked, we know that it must come from that button. It is actually OK to have one listener listening to many buttons. In this case, we need a way of finding which button has been pressed.
3
Writing a Listener as implementing an interface We have talk about three ways of writing a listener: – creating a separate class that implements the listener's interface – using inner class that implements the listener's interface – using anonymous class
4
Writing a Listener as implementing an interface There is another way of writing a Listener. For example, if you are using a subclass of JFrame, then you can actually make this subclass implement the listener interface. Then, the subclass can then have two uses: – used as a frame – used as a listener
5
Writing a Listener as implementing an interface public class FrameListener extends JFrame implements ActionListener { private JButton button;...... public FrameListener() { button=new JButton(); button.addActionListener(this); }.... public void actionPerformed(ActionEvent e) {.......... }
6
Writing a Listener as implementing an interface This method has one limitation: – it can only be used to define one action listener. So if you have a frame that has a number of buttons that act differently, you can still have one action listener. This means you need to include some checking in the actionPerformed method so as to act differently if the source is different.
7
One listener for many buttons The actionPerformed method accepts one parameter of type ActionEvent. The class ActionEvent has the following methods: – public Object getSource() This method returns the source of the event. So if the source is a button, you can get the button using the following statement in actionPerformed():
8
One listener for many buttons public void actionPerformed(ActionEvent e) { JButton button=(JButton)e.getSource();....... } – String getCommand() This method return the text of the button that generate the event.
9
One listener for many buttons Now, assume that we have the following frame:
10
One listener for many buttons where the buttons are stored in an array: JButton button[] Now, we want to have an action listener so that when a button is pressed, the corresponding number represented by the button would be appended to the text in the JTextField.
11
One listener for many buttons Since the action for all the buttons are the same, it is reasonable to use only one action listener for the 10 buttons. Now, let jTextField1 be the textfield. Then, the actionPerformed method of the action listener is:
12
One listener for many buttons...... ActionListener listener=new ActionListener() { public void actionPerformed(ActionEvent e) { String command=e.getActionCommand(); jTextField1.setText(jTextField1.getText()+command); } };.... for (int i=0;i<10;i++) { button[i].addActionListener(listener); }.....
13
JTextField We have already used the most important methods: – String getText(); – void setText(String string);
14
JCheckBox For JCheckBox, you need to add an ItemListener to it so that when a user checks or unchecks the box, the itemStateChanged method will be invokded: public void itemStateChanged(ItemEvent e)
15
JCheckBox ItemEvent has a method which return the current state of the check box: – int getStateChanged() the returned value can be ItemEvent.DESELECTED or ItemEvent.SELECTED.
16
JCheckBox Again, you can use one listener to serve a number of JCheckBox's. You can then use the getSource method of the ItemEvent to get the original JCheckBox that generates this event.
17
JCheckBox Now, assume that we have the following frame: The frame has two JCheckBox's named cleverBox and hardWorkingBox.
18
ItemListener listener=new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getSource()==hardWorkingBox) { if (e.getStateChange()==ItemEvent.SELECTED) { System.out.println("I am hardworking"); } else { System.out.println("I am not hardworking"); } } else if (e.getSource()==cleverBox) { if (e.getStateChange()==ItemEvent.SELECTED) { System.out.println("I am clever"); } else { System.out.println("I am not clever"); } } };
19
JRadioButton JRadioButton is like JCheckBox except that you can JRadioButton can be put into a ButtonGroup so that at most one of the button can be on. This means if one button is on and you press another button in the group, then the originally on button will be off.
20
JRadioButton You need to add an ItemListener to a JRadioButton to listen for ItemEvent. When you click on a button, there can be two events, the first is the selection of the one that you just clicked, the second is the deselection of the original on button. So if you are using one listeners for all the buttons in a group, you need to check the source of the event and the type of the event, ie, a select event or a deselect even.
21
JRadioButton Consider the following frame: The frame has two JRadioButton's named hardworking and notHardworking
22
public class RadioFrame extends javax.swing.JFrame implements java.awt.event.ItemListener { private javax.swing.ButtonGroup group=new javax.swing.ButtonGroup(); public void itemStateChanged(java.awt.event.ItemEvent e) { if (e.getSource()==hardworking && e.getStateChange()==java.awt.event.ItemEvent.SELECTED) {....... } public RadioFrame() {...... group.add(hardworking); hardworking.addItemListener(this); group.add(notHardworking); notHardworking.addItemListener(this); }
23
JComboBox A JComboBox is a pull down menu that allows users to select an item from a list. The list can be provided as in a constructor as an array or be added using the addItem method If the JComboBox is set editable, then, the user can type in the choice using the keyboard.
24
JComboBox You need to add an ActionListener to a JComboBox so that when the user selects an item in the pull down menu or press enter in an editable JComboBox, the actionPerformed method will be invoked. Then, you need to use the getSource method of ActionEvent to get the source of the event. Then, you need to invoke the getSelectedItem to get the object that the user has selected.
25
JComboBox Usually, the objects to be placed are Strings. However, they can be any types of objects.
26
JComboBox Consider the following frame: The frame has a JComboBox called comboBox.
27
public class ComboFrame extends javax.swing.JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { JComboBox box=(JComboBox)e.getSource(); String st=(String)box.getSelectedItem(); System.out.println(st); } public ComboFrame() { comboBox = new javax.swing.JComboBox(new String[]{"Apple","Orange","Banana"}); comboBox.addActionListener(this);...... }....... }
28
Drawing Each GUI entity has the following method: public void paint(Graphics g) If you want to draw something on the GUI entity, you can override this method. Graphics is a graphic context. It provide many methods to draw varies things like line, shapes and text.
29
Drawing Usually, the first thing to do in the paint method is to invoke the paint method of the super class. This is to make sure that the entity is drawn properly. Then, you can add your own drawing later. So a typical paint method would look like this: public void paint(Graphics g) { super(g);...... }
30
Drawing Graphics provides the following methods: – drawLine: to draw a line – drawRect: to draw a rectangle – drawOval: to draw an oval – fillRect: to draw a filled rectangle – fillOval: to draw a filled oval – drawString: to draw a string
31
Draw Consider the following frame: The frame has a subclass of JPanel. We are going to override the paint method of this class to draw the cross.
32
Drawing public class Panel extends javax.swing.JPanel { public void paint(Graphics g) { super.paint(g); g.drawLine(10, 10, 30, 30); g.drawLine(10,30, 30, 10); } public Panel() { setPreferredSize(new java.awt.Dimension(100, 100));..... } }
33
Drawing The paint method is never called by a user. It is called by the Java runtime system whenever the GUI item has to be redrawn. For example, when the item is originally covered is becoming viewable.
34
Drawing If during the execution of the program, something different is to be drawn on an GUI entity, it may not be shown immediately on the screen. In order to show the change immediately, you need to invoke the repaint method: public void repaint()
35
Drawing Note that the method does not require any parameter. So, it will then invoke the paint method to draw the GUI entity.
36
Color This class is used to specify the color to be used. For example, if we change the paint method in the last program: public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.drawLine(10, 10, 30, 30); g.drawLine(10,30, 30, 10); }
37
Color For a GUI entity, you can use the setForeground and setBackground method to set the foreground colour and the background colour of the entity. For example for the radio button used previously, if we add the following statements: hardworking.setForeground(Color.red); hardworking.setBackground(Color.blue);
38
Color We have used something like Color.red, etc to represent the red color. In fact, you can find other predefined color like black, blue and many more. You can also create your own colour using the following constructor of Color: public Color(int r, int g, int b)
39
Font You can also set the font to be used in a GUI item. The following constructor is available: public Font(String name, int style, size) name can be Dialog, DialogInput, Monospaced, Serif or SansSerif. The style argument is an integer bitmask that may be PLAIN, or a bitwise union of BOLD and/or ITALIC
40
Font So we can use the following statement to change the font to be used: hardworking.setFont(new Font("Serif", Font.ITALIC, 28));
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.