import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionListenerTest ...{ public static void main(String[] args) ...{ JFrame frame = new JFrame("Button Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton jbClose = new JButton("Close the Frame"); jbClose.addActionListener(new ActionListener () ...{ public void actionPerformed(ActionEvent e) ...{ if (e.getSource().equals(jbClose)) ...{ System.exit(0); } } } ); frame.add(jbClose); frame.pack(); frame.setVisible(true); } } |
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ButtonFrame extends JFrame implements ActionListener ...{ JButton jbClose = null; public ButtonFrame() ...{ super("ButtonFrame Test"); jbClose = new JButton ("Close the Frame in ButtonFrame"); jbClose.addActionListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(jbClose); this.pack(); this.setVisible(true); } public void actionPerformed(ActionEvent e) ...{ if (e.getSource().equals(jbClose)) ...{ System.exit(0); } } public static void main(String[] args) ...{ ButtonFrame bf = new ButtonFrame(); } } |