CBOP3203
What Is an Event? ◦ Events – Objects that describe what happened ◦ Event Sources – The generator of an event ◦ Event Handlers – A method that receives an event object, deciphers it, and processes the user’s interaction. The user clicks on the button ActionEvent Some event handler actionPerformed(ActionEvent e) { }
Event Delegation Model describes the steps that need to be followed by a programmer in order to insert codes for event handling in a program. With this model, events are sent to the component from which the event originated, but it is up to each component to propagate the event to one or more registered classes called listeners. Listeners contain event handlers that receive and process the event. In this way, the event handler can be in an object separate from the component. Listeners are classes that implement the EventListener interface.
An event can be sent to many event handlers. Event handlers register with components when they are interested in events generated by that component.
Client objects (handlers) register with a GUI component that they want to observe GUI components trigger only the handlers for the type of event that has occurred Most components can trigger more than one type of event. The delegation model distributes the work among multiple classes.
import javax.swing.*; import java.awt.*; Make sure the import java.awt.event.*; java.awt.event.* package is imported public class btnListener extends JApplet implements ActionListener { public void init(){ Container conpane=getContentPane(); conpane.setLayout(new FlowLayout()); Use the listener interface together JButton btnTest=new JButton("Press Me"); with the implements btnTest.addActionListener(this); statements conpane.add(btnTest); } public void actionPerformed(ActionEvent e) { JOptionPane . showMessageDialog( null , "Why did you press me?" ); } }
Save this class as colorApplet.java import javax.swing.*; import java.awt.*; public class colorApplet extends JApplet { public void init( ){ Container conpane=getContentPane( ); conpane.add(new Pattern()); } }
Save this class as import javax.swing.*; import java.awt.*; Pattern.java import java.awt.event.*; public class Pattern extends JPanel implements ActionListener { private JButton btnRed,btnGreen,btnBlue; public Pattern() { btnRed = new JButton("red"); implements btnGreen = new JButton("green"); ActionListener btnBlue = new JButton("blue"); btnRed.addActionListener(this); btnGreen.addActionListener(this); btnBlue.addActionListener(this); add(btnRed); add(btnGreen); add(btnBlue); } public void actionPerformed(ActionEvent e) { Object source = e.getSource( ); When the Button object is clicked Color clr = Color.white; on with the mouse, an ActionEvent if (source==btnRed) is sent. The ActionEvent is received clr = Color.red; through the actionPerformed() else if (source==btnGreen) clr = Color.green; method of any ActionListener that else if (source==btnBlue) is registered on the button through clr = Color.blue; its addActionListener() method setBackground(clr); repaint(); } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calculateApplet extends JApplet implements ActionListener { JTextField txt1,txt2,txt3; JLabel lbl1,lbl2,lbl3; public void init( ) { Container conpane = getContentPane( ); conpane.setLayout(new GridLayout(3,2)); lbl1 = new JLabel("First Number"); lbl2 = new JLabel("Second Number"); lbl3 = new JLabel("Sum of two integers"); txt1 = new JTextField("0" , 10); txt2 = new JTextField("0" , 10); txt3 = new JTextField("0" , 10); txt1.addActionListener(this); txt2.addActionListener(this); txt3.addActionListener(this); conpane.add(lbl1); conpane.add(txt1); conpane.add(lbl2); conpane.add(txt2); conpane.add(lbl3); conpane.add(txt3); } public void actionPerformed(ActionEvent e) { int num1 = Integer . parseInt(txt1.getText( )); int num2 = Integer . parseInt(txt2.getText( )); int num3 = num1 + num2; txt3.setText(String . valueOf(num3)); } }
import javax.swing.*; import java.awt.*; There are a few components that import java.awt.event.*; use the ItemListener of the event public class Alphabet extends JApplet implements ItemListener { private JTextField txtInput; listener. Among the components private JCheckBox chkBold,chkItalic; public void init( ){ used are the JCheckBox and JList. Container conpane = getContentPane( ); The ItemListener performs the conpane.setLayout(new FlowLayout( )); txtInput = new JTextField("" , 20); itemStateChanged method to chkBold = new JCheckBox("Bold" , false); chkItalic = new JCheckBox("Italic" , false); implement action to the events. chkBold.addItemListener(this); While addItemListener is used to chkItalic.addItemListener(this); conpane.add(txtInput); register the component. conpane.add(chkBold); conpane.add(chkItalic); } public void itemStateChanged(ItemEvent e){ int t= 0 ,c= 0 ; Font font; if (chkBold.isSelected( )) t = Font.BOLD; if (chkItalic.isSelected( )) { c = Font.ITALIC; } font = new Font("Serif",t + c, 14); txtInput.setFont(font); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RGBColor extends JApplet implements AdjustmentListener { private JLabel lblRed; private JLabel lblGreen; private JLabel lblBlue; private JScrollBar barRed; private JScrollBar barGreen; private JScrollBar barBlue; private JPanel panelColor; public void init() { Container conpane = getContentPane(); JPanel p = new JPanel( ); p.setLayout(new GridLayout(3, 2)); lblRed = new JLabel("Red 0"); p.add(lblRed); barRed=new JScrollBar(Adjustable.HORIZONTAL, 0,0, 0, 255); p.add(barRed); barRed.setBlockIncrement(16); barRed.addAdjustmentListener(this); The java file continues on next page
lblGreen = new JLabel("Green 0"); p.add(lblGreen); barGreen = new JScrollBar(Adjustable.HORIZONTAL , 0, 0, 0,255); p.add(barGreen); barGreen.setBlockIncrement(16); barGreen.addAdjustmentListener(this); lblBlue = new JLabel("Blue 0"); p.add(lblBlue); barBlue= new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0,255); p.add(barBlue); barBlue.setBlockIncrement(16); barBlue.addAdjustmentListener(this); conpane.add(p, "South"); panelColor = new JPanel(); panelColor.setBackground(new Color( 0, 0, 0)); conpane.add(panelColor, "Center"); } public void adjustmentValueChanged(AdjustmentEvent evt){ lblRed.setText("Red " + barRed.getValue()); lblGreen.setText("Green " + barGreen.getValue()); lblBlue.setText("Blue " + barBlue.getValue()); panelColor.setBackground(new Color(barRed.getValue(), barGreen.getValue( ), barBlue.getValue())); panelColor.repaint(); } }
Q1. Write a temperature conversion applet that converts from Fahrenheit to Celsius. The Fahrenheit temperature should be entered from the keyboard (via a JTextField). A JLabel should be used to display the converted temperature. Use the following formula for the conversion:
Q2. Enhance the temperature conversion applet of Q1 by adding the Kelvin temperature scale. The applet should also allow the user to make conversions between any two scales. Use the following formula for the conversion between Kelvin and Celsius (in addition to the formula in Q1):
Recommend
More recommend