/* Simple demo which creates two buttons and prints messages when they are clicked Change: 1/11/98 Kim Bruce converted to Java 1.1 event model */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo extends JFrame { protected JButton startButton, stopButton; // Constructor sets features of the frame, creates buttons, adds them to the frame, and // assigns an object to listen to them public ButtonDemo() { super("Button demo"); // set title bar setSize(400,200); // sets the size of the window startButton = new JButton("Start"); // create two new buttons w/labels start and stop stopButton = new JButton("Stop"); startButton.setBackground(Color.green); // set backgrounds of buttons stopButton.setBackground(Color.red); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); // sets layout so objects added go from left to right // until fill up row and then go to next row contentPane.add(startButton); contentPane.add(stopButton); // create objects to listen to each button (separately): startButton.addActionListener(new StartButtonListener()); stopButton.addActionListener(new StopButtonListener()); } // Trivial main program associated with new window public static void main(String[] main){ ButtonDemo app = new ButtonDemo(); // Create an instance of Buttons app.show(); // Show it on the Mac screen } // -- leave this out and you won't see it! // Listener for start button protected class StartButtonListener implements ActionListener{ // print "start button" when button pushed public void actionPerformed(ActionEvent evt) { System.out.println("Start button"); } } // Listener for stop button protected class StopButtonListener implements ActionListener{ // print "stop button" when button pushed public void actionPerformed(ActionEvent evt) { System.out.println("Stop button"); } } }