谁能告诉我为什么这段代码编译没有错误,但是运行的时候没有界面呢????import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class CalculatorFrame extends JFrame implements ActionListener
{
private JTextField text;
  private JButton button_0,button_1,button_2,button_3,button_4,button_5,button_6,button_7,button_8,button_9;
private JButton button_plus,button_minus,button_mult,button_div,button_equal,button_cancel;
public CalculatorFrame()
{
super("计算器");
this.setSize(256,240);
this.setBackground(Color.lightGray);
this.setLocation(300,240);
this.setLayout(new java.awt.GridLayout(4,4));

text=new JTextField(40);
text.setEditable(false);
this.add(text);

button_0=new JButton("0");
button_1=new JButton("1");
button_2=new JButton("2");
button_3=new JButton("3");
button_4=new JButton("4");
button_5=new JButton("5");
button_6=new JButton("6");
button_7=new JButton("7");
button_8=new JButton("8");
button_9=new JButton("9");
button_plus=new JButton("加");
button_minus=new JButton("减");
button_mult=new JButton("乘");
button_div=new JButton("除");
button_equal=new JButton("=");
button_cancel=new JButton("清除");

this.add(button_0);
this.add(button_1);
this.add(button_2);
this.add(button_3);
this.add(button_4);
this.add(button_5);
this.add(button_6);
this.add(button_7);
this.add(button_8);
this.add(button_9);
this.add(button_plus);
this.add(button_minus);
this.add(button_mult);
this.add(button_div);
this.add(button_equal);
this.add(button_cancel);

button_0.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
button_9.addActionListener(this);
button_plus.addActionListener(this);
button_minus.addActionListener(this);
button_mult.addActionListener(this);
button_div.addActionListener(this);
button_cancel.addActionListener(this);
this.addWindowListener(new WinClose());
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button_cancel)
text.setText("");
else
{

}

}
public static void main(String arg[])
{
new CalculatorFrame();
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

解决方案 »

  1.   

    我用JCreator Pro 你们用什么呢
      

  2.   

    eclipse,其实ide没有什么关系,你的jdk版本号?
      

  3.   

    我用1.4的JDK运行报如下错误,Lz,不需要我告诉你这是啥错吧,呵呵...Exception in thread "main" java.lang.Error: Do not use CalculatorFrame.setLayout() use CalculatorFrame.getContentPane().setLayout() instead
            at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
            at javax.swing.JFrame.setLayout(JFrame.java:531)
            at CalculatorFrame.<init>(CalculatorFrame.java:16)
            at CalculatorFrame.main(CalculatorFrame.java:86)
      

  4.   

    帮你改好了import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class CalculatorFrame extends JFrame implements ActionListener {
    private JTextField text;
    private JButton button_0, button_1, button_2, button_3, button_4, button_5,
    button_6, button_7, button_8, button_9;
    private JButton button_plus, button_minus, button_mult, button_div,
    button_equal, button_cancel; public CalculatorFrame() {
    super("计算器");
    this.setSize(256, 240);
    this.setBackground(Color.lightGray);
    this.setLocation(300, 240);
    this.getContentPane().setLayout(new java.awt.GridLayout(4, 4)); text = new JTextField(40);
    text.setEditable(false);
    this.add(text); button_0 = new JButton("0");
    button_1 = new JButton("1");
    button_2 = new JButton("2");
    button_3 = new JButton("3");
    button_4 = new JButton("4");
    button_5 = new JButton("5");
    button_6 = new JButton("6");
    button_7 = new JButton("7");
    button_8 = new JButton("8");
    button_9 = new JButton("9");
    button_plus = new JButton("加");
    button_minus = new JButton("减");
    button_mult = new JButton("乘");
    button_div = new JButton("除");
    button_equal = new JButton("=");
    button_cancel = new JButton("清除"); this.add(button_0);
    this.add(button_1);
    this.add(button_2);
    this.add(button_3);
    this.add(button_4);
    this.add(button_5);
    this.add(button_6);
    this.add(button_7);
    this.add(button_8);
    this.add(button_9);
    this.add(button_plus);
    this.add(button_minus);
    this.add(button_mult);
    this.add(button_div);
    this.add(button_equal);
    this.add(button_cancel); button_0.addActionListener(this);
    button_1.addActionListener(this);
    button_2.addActionListener(this);
    button_3.addActionListener(this);
    button_4.addActionListener(this);
    button_5.addActionListener(this);
    button_6.addActionListener(this);
    button_7.addActionListener(this);
    button_8.addActionListener(this);
    button_9.addActionListener(this);
    button_plus.addActionListener(this);
    button_minus.addActionListener(this);
    button_mult.addActionListener(this);
    button_div.addActionListener(this);
    button_cancel.addActionListener(this);
    this.addWindowListener(new WinClose());
    this.setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button_cancel)
    text.setText("");
    else { } } public static void main(String arg[]) {
    new CalculatorFrame();
    }
    }class WinClose extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    }
      

  5.   

    我的JDK1.4也不行,同样的问题,看来要更新一下了,有没有1.6的链接,给个