liuxinghua21
2017-11-01 15:15课本上的代码写入后出现错误(无法从静态上下文中引用非静态变量),代码如下
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Mypanel extends JPanel implements ActionListener
{
JButton button1,button2,button3;
Color backColor;
Mypanel()
{
button1=new JButton("确定");
button2=new JButton("取消");
button3=new JButton("保存");
add(button1);add(button2);add(button3);
setBackground(Color.pink);
backColor=getBackground();
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
setBackground(Color.cyan);
else if(e.getSource()==button2)
setBackground(backColor);
}
public class Ex5_4 extends JFrame
{
Mypanel panel1,panel2,panel3;
JButton button;
Ex5_4()
{
setSize(300,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
panel1=new Mypanel();panel2=new Mypanel();panel3=new Mypanel();
button=new JButton("我不在那些面板里");
add(panel1);add(panel2);add(panel3);
add(button);
validate();
}
}
public static void main(String args[])
{
Ex5_4 win=new Ex5_4();
}
}
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- python中关于描述符类的问题
- python
- 1个回答
- 为什么会调用拷贝构造函数与析构函数
- c++
- 1个回答
- MySQL语句和课本一样还报错,为啥?
- sql
- mysql
- 1个回答
- C++结构体如何定义构造函数??
- c语言
- c++
- 2个回答
- AI八数码问题 Stack Overflow
- c
- 0个回答