liuxinghua21
liuxinghua21
2017-11-01 15:15

课本上的代码写入后出现错误(无法从静态上下文中引用非静态变量),代码如下

  • java

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条回答