把Panel textPanel=new Panel();一句移到外面如下: public class PanelDemo extends Frame implements ActionListener { public static final int WIDTH=300; public static final int HEIGHT=200; Panel textPanel=new Panel();
public PanelDemo() { setTitle("Panel Demonstration"); setSize(WIDTH,HEIGHT); setBackground(Color.blue); addWindowListener(new WindowDestroyer());
1:注意变量访问权限,2:addWindowListener(new WindowDestroyer());语句用的时候没有声明接口吧 import java.awt.*; import java.awt.event.*;public class PanelDemo extends Frame implements ActionListener { public static final int WIDTH=300; public static final int HEIGHT=200; TextField theText; Button goButton,stopButton; Panel buttonPanel,textPanel;
public PanelDemo() { setTitle("Panel Demonstration"); setSize(WIDTH,HEIGHT); setBackground(Color.blue); //addWindowListener(new WindowDestroyer());
public class PanelDemo extends Frame implements ActionListener
{
public static final int WIDTH=300;
public static final int HEIGHT=200;
Panel textPanel=new Panel();
public PanelDemo()
{
setTitle("Panel Demonstration");
setSize(WIDTH,HEIGHT);
setBackground(Color.blue);
addWindowListener(new WindowDestroyer());
Panel buttonPanel=new Panel();
buttonPanel.setBackground(Color.white);
buttonPanel.setLayout(new FlowLayout());
Button goButton=new Button("Green");
goButton.addActionListener(this);
buttonPanel.add(goButton);
Button stopButton=new Button("Red");
stopButton.addActionListener(this);
buttonPanel.add(stopButton);
textPanel.setBackground(Color.black);
TextField theText=new TextField(30);
theText.setBackground(Color.yellow);
theText.setText("watch me");
textPanel.add(theText);
setLayout(new BorderLayout());
add(buttonPanel,"South");
add(textPanel,"North");
}
public static void main(String[] args)
{
PanelDemo guiWithPanel=new PanelDemo();
guiWithPanel.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Red"))
{
theText.setBackground(Color.red);
theText.setText("stop");
}
else if(e.getActionCommand().equals("Green"))
{
theText.setBackground(Color.green);
theText.setText("go");
}
else
theText.setText("interface error!");
repaint();
}
private TextField theText;
private Panel textPanel;
}
感谢帮忙,楼上的前辈,还是一样的错误,编译没有问题,执行时才会出现问题,我是用的命令行来运行和编译的。
我用PANEL类编了另外一个计算器的程序,可以正确运行,但是还是会出现如上情况。请大虾指点,不胜感激。
在调用ACTIONLISTENER的时候不能用来处理调用对象以外的数据,比如:
theText.setBackground(Color.red);就不对,因为它处理了theText对象,而调用ACTIONLISTENER的是stopButton和goButton对象。
不知道理解得对不对,如果是这样的话,那ACTIONLISTENER的作用就很小了。
import java.awt.*;
import java.awt.event.*;public class PanelDemo extends Frame implements ActionListener
{
public static final int WIDTH=300;
public static final int HEIGHT=200;
TextField theText;
Button goButton,stopButton;
Panel buttonPanel,textPanel;
public PanelDemo()
{
setTitle("Panel Demonstration");
setSize(WIDTH,HEIGHT);
setBackground(Color.blue);
//addWindowListener(new WindowDestroyer());
buttonPanel=new Panel();
buttonPanel.setBackground(Color.white);
buttonPanel.setLayout(new FlowLayout());
goButton=new Button("Green");
goButton.addActionListener(this);
buttonPanel.add(goButton);
stopButton=new Button("Red");
stopButton.addActionListener(this);
buttonPanel.add(stopButton);
textPanel=new Panel();
textPanel.setBackground(Color.black);
theText=new TextField(30);
theText.setBackground(Color.yellow);
theText.setText("watch me");
textPanel.add(theText);
setLayout(new BorderLayout());
add(buttonPanel,"South");
add(textPanel,"North");
}
public static void main(String[] args)
{
PanelDemo guiWithPanel=new PanelDemo();
guiWithPanel.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==goButton)
{
theText.setBackground(Color.red);
theText.setText("stop");
}
else if(e.getSource()==stopButton)
{
theText.setBackground(Color.green);
theText.setText("go");
}
else
theText.setText("interface error!");
repaint();
}
}