import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
public class JButtonTest extends JFrame
{
private static final long serialVersionUID = 7794841109938881749l;
public JButtonTest()
{
add(new MyPanel()); //添加一个面板,具体功能由面板实现
pack(); //有系统自动调节框架的大小尺寸
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //使程序能正常关闭
setVisible(true); //使框架可见
}
public static void main(String[] args)
{
new JButton();
}
class MyPanel extends JPanel
{
private static final long serialVersionUID = 7794841109938881749l;
public MyPanel()
{
setPreferredSize(new Dimension(400, 300));
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
JButton yellowButton = new JButton("Yellow");
blueButton.addActionListener(new ChangeBackColorAction(Color.BLUE));
redButton.addActionListener(new ChangeBackColorAction(Color.RED));
yellowButton.addActionListener(new ChangeBackColorAction(Color.YELLOW));
add(blueButton); //将3个按钮添加到面板上
add(redButton);
add(yellowButton);
}
//Action类是Panel类的内部类,目的是可以调用Panel的方法
private class ChangeBackColorAction implements ActionListener
{
Color c;
public ChangeBackColorAction(Color x)
{
c = x;
}
public void actionPerformed(ActionEvent e)
{
setBackground(c);
}
}
}
}又出现这个问题,上次也是这样。代码编译也没错,不知道是什么问题,大虾帮帮忙。
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
public class JButtonTest extends JFrame
{
private static final long serialVersionUID = 7794841109938881749l;
public JButtonTest()
{
add(new MyPanel()); //添加一个面板,具体功能由面板实现
pack(); //有系统自动调节框架的大小尺寸
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //使程序能正常关闭
setVisible(true); //使框架可见
}
public static void main(String[] args)
{
new JButton();
}
class MyPanel extends JPanel
{
private static final long serialVersionUID = 7794841109938881749l;
public MyPanel()
{
setPreferredSize(new Dimension(400, 300));
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
JButton yellowButton = new JButton("Yellow");
blueButton.addActionListener(new ChangeBackColorAction(Color.BLUE));
redButton.addActionListener(new ChangeBackColorAction(Color.RED));
yellowButton.addActionListener(new ChangeBackColorAction(Color.YELLOW));
add(blueButton); //将3个按钮添加到面板上
add(redButton);
add(yellowButton);
}
//Action类是Panel类的内部类,目的是可以调用Panel的方法
private class ChangeBackColorAction implements ActionListener
{
Color c;
public ChangeBackColorAction(Color x)
{
c = x;
}
public void actionPerformed(ActionEvent e)
{
setBackground(c);
}
}
}
}又出现这个问题,上次也是这样。代码编译也没错,不知道是什么问题,大虾帮帮忙。
{
new JButtonTest();
}