*********************************** * ActionExample1.java Inherits from CloseableFrame.java and uses SetSizeButton.java. * ActionExample2.java Inherits from CloseableFrame.java. ********************************************************** ActionExample1.java ******************* import java.awt.*; public class ActionExample1 extends CloseableFrame { public static void main(String[] args) { new ActionExample1(); } public ActionExample1() { super("Handling Events in Component"); setLayout(new FlowLayout()); setFont(new Font("Serif", Font.BOLD, 18)); add(new SetSizeButton(300, 200)); add(new SetSizeButton(400, 300)); add(new SetSizeButton(500, 400)); setSize(400, 300); setVisible(true); } } ******************** CloseableFrame.java ******************** import java.awt.*; import java.awt.event.*; /** A Frame that you can actually quit. Used as the starting * point for most Java 1.1 graphical applications. *********************** public class CloseableFrame extends Frame { public CloseableFrame(String title) { super(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } /** Since we are doing something permanent, we need * to call super.processWindowEvent first. */ public void processWindowEvent(WindowEvent event) { super.processWindowEvent(event); // Handle listeners. if (event.getID() == WindowEvent.WINDOW_CLOSING) { // If the frame is used in an applet, use dispose(). System.exit(0); } } } ********************** SetSizeButton.java ********************** import java.awt.*; import java.awt.event.*; /////////////////////// public class SetSizeButton extends Button implements ActionListener { private int width, height; public SetSizeButton(int width, int height) { super("Resize to " + width + "x" + height); this.width = width; this.height = height; addActionListener(this); } public void actionPerformed(ActionEvent event) { getParent().setSize(width, height); getParent().invalidate(); getParent().validate(); } } *********************** ActionExample2.java *********************** import java.awt.*; import java.awt.event.*; /***********/ public class ActionExample2 extends CloseableFrame implements ActionListener { public static void main(String[] args) { new ActionExample2(); } private Button button1, button2, button3; public ActionExample2() { super("Handling Events in Other Object"); setLayout(new FlowLayout()); setFont(new Font("Serif", Font.BOLD, 18)); button1 = new Button("Resize to 300x200"); button2 = new Button("Resize to 400x300"); button3 = new Button("Resize to 500x400"); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); add(button1); add(button2); add(button3); setSize(400, 300); setVisible(true); } public void actionPerformed(ActionEvent event) { if (event.getSource() == button1) { updateLayout(300, 200); } else if (event.getSource() == button2) { updateLayout(400, 300); } else if (event.getSource() == button3) { updateLayout(500, 400); } } private void updateLayout(int width, int height) { setSize(width, height); invalidate(); validate(); } } ****************/> CloseableFrame.java. ****************/>< import java.awt.*; import java.awt.event.*; /** A Frame that you can actually quit. Used as the starting * point for most Java 1.1 graphical applications. * ********************* public class CloseableFrame extends Frame { public CloseableFrame(String title) { super(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } /** Since we are doing something permanent, we need * to call super.processWindowEvent first. */ public void processWindowEvent(WindowEvent event) { super.processWindowEvent(event); // Handle listeners. if (event.getID() == WindowEvent.WINDOW_CLOSING) { // If the frame is used in an applet, use dispose(). System.exit(0); } } } ***************
Handling Events
Permanent link to this article: http://bangla.sitestree.com/handling-events/