package button;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrameTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame=new ButtonFrame();
frame.setTitle("ButtonFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
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.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);
}
}
}
編譯沒問題,把你的編譯錯誤貼出來