/*
Chapter 7 Sample Program: Creating buttons w/ Button Handler using 2 classes.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ChangeFont extends JFrame implements ActionListener
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 50;
private JButton okButton;
private JLabel promptText1;
public ChangeFont( )
{
setTitle("Sample Frame with Button Events");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setResizable(false);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
okButton = new JButton("Ok");
okButton.setBounds(110, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
okButton.setFont(new Font("Serif", Font.PLAIN, 18));
contentPane.add(okButton);
promptText1 = new JLabel();
promptText1.setText("Code:");
promptText1.setBounds(20, 20, 150, 50); // 30 from left, 20 from top, 150 length, 25 height
promptText1.setFont(new Font("Serif", Font.BOLD, 48));
promptText1.setForeground(Color.red);
contentPane.add(promptText1);
okButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
setTitle("You clicked " + buttonText);
}
public static void main(String[] args)
{
ChangeFont frame = new ChangeFont();
frame.setVisible(true);
}
} // End of class