import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrameTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame bf=new ButtonFrame();
bf.setTitle("ButtonFrame");
bf.setDefaultCloseOperation(ButtonFrame.EXIT_ON_CLOSE);
bf.setVisible(true);
}
});
}
}
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonFrame extends JFrame{ private JPanel buttonPanel; private static final int DEFAULT_WIDTH=300; private static final int DEFAULT_HEIGHT=200; public ButtonFrame() { setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); JButton yellowButton=new JButton("Yellow"); JButton blueButton=new JButton("Blue"); JButton redButton=new JButton("Red"); buttonPanel=new JPanel(); buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); add(buttonPanel); ColorAction yellowAction=new ColorAction(Color.YELLOW); ColorAction blueAction=new ColorAction(Color.BLUE); ColorAction redAction=new ColorAction(Color.red); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } private class ColorAction implements ActionListener { private Color backgroundColor; public ColorAction(Color c) { backgroundColor=c; } public void actionPerformed(ActionEvent event) { buttonPanel.setBackground(backgroundColor); } } }