窗口三个按钮程序的错误,请帮忙解决!(书中原例子)

ladofwind ladofwind 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest{
public static void main(String []args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300,300);

ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
}

class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton b1=new JButton("Yellow");
JButton b2=new JButton("Blue");
JButton b3=new JButton("Red");

add(b1);
add(b2);
add(b3);

ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction redAction=new ColorAction(Color.red);

b1.addActionListener(yellowAction);
b2.addActionListener(blueAction);
b3.addActionListener(redAction);
}

}

class ColorAction implements ActionListener
{
private Color backgroundColor;
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{

setBackground(Color.yellow);//错误出现处~~
}

}
...全文
评论 4
收藏 已收藏
分享
返回顶部
ladofwind
Java SE 62,621 成员 307,251 内容