SimpleButton3
File: java/SimpleButton3/Main.java
/*
* Simple button program, with 2 buttons
* R. Jacob 9/11/2001
*/
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main (String [] args) {
new Main ();
}
public Main () {
// Window setup
setSize (300, 300);
Container content = getContentPane();
content.setLayout (new FlowLayout());
// Put a button in
MyButton b1 = new MyButton ("Push me");
content.add (b1);
// Put another button in
MyButton b2 = new MyButton ("Push me also");
content.add (b2);
// Show the window
setVisible (true);
}
}
File: java/SimpleButton3/MyButton.java
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class MyButton extends JButton implements ActionListener {
public MyButton (String label) {
setText (label);
addActionListener (this);
}
public void actionPerformed(ActionEvent e) {
System.out.println ("Button was pushed");
}
}