代码修改如下:
自己分析原因import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class javaButton
{
public static void main(String[] args)
{
ButtonTestFrame frame = new ButtonTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class ButtonTestFrame extends JFrame
{
public ButtonTestFrame()
{
setTitle("Button");
setSize(200, 400); ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
ColorAction yellowAction = new ColorAction(Color.yellow,this);
yellowButton.addActionListener(yellowAction);
}
}class ColorAction implements ActionListener
{
public ColorAction(Color back,JPanel backpane)
{
backgroundColor = back;
bpane = (ButtonPanel)backpane;
} public void actionPerformed(ActionEvent event)
{ bpane.setBackground(backgroundColor);
bpane.repaint();
} private Color backgroundColor;
private ButtonPanel bpane;
}
自己分析原因import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class javaButton
{
public static void main(String[] args)
{
ButtonTestFrame frame = new ButtonTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class ButtonTestFrame extends JFrame
{
public ButtonTestFrame()
{
setTitle("Button");
setSize(200, 400); ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
ColorAction yellowAction = new ColorAction(Color.yellow,this);
yellowButton.addActionListener(yellowAction);
}
}class ColorAction implements ActionListener
{
public ColorAction(Color back,JPanel backpane)
{
backgroundColor = back;
bpane = (ButtonPanel)backpane;
} public void actionPerformed(ActionEvent event)
{ bpane.setBackground(backgroundColor);
bpane.repaint();
} private Color backgroundColor;
private ButtonPanel bpane;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货