当前位置: 代码迷 >> Java相关 >> 求纠正一下程序
  详细解决方案

求纠正一下程序

热度:142   发布时间:2010-10-24 23:52:57.0
求纠正一下程序
import javax.swing.*;
public class Qiuhe  extends JFrame{
    JFrame a =new JFrame("求和工具");
    JPanel panel=new JPanel();
    JTextField field1=new JTextField(8);
     JButton button1=new JButton("+");
     JTextField field2=new JTextField(8);
     JButton button2=new JButton("=");
     JTextField field3=new JTextField(8);
     public Qiuhe()
     {
         panel.add(field1);
         panel.add(button1);
         panel.add(field2);
         panel.add(button2);
         panel.add(field3);
         a.add(panel);
          button2.addActionListener(new java.awt.event.ActionListener()
          { public void actionPerformed(java.awt.event.ActionEvent evt)
           {
            int x=Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
            field3.setText(Integer.toString(x));
           }
          });
     }
public static void main(String[] args)
{
   
Qiuhe b=new Qiuhe();
b.setTitle("求和工具");
b.pack();
b.setDefaultCloseOperation(EXIT_ON_CLOSE);
b.setSize(400,100);
b.setVisible(true);
}
}程序没有错,但是为什么运行是结果出现的文本框不是计算器那样的呢?
搜索更多相关的解决方案: public  import  

----------------解决方案--------------------------------------------------------
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Qiuhe extends JFrame {
//    JFrame a = new JFrame("求和工具");  你写的Qiuhe类已经继承了Jframe了,所以没有必要再定义一个JFrame的成员变量
    JPanel panel = new JPanel();
    JTextField field1 = new JTextField(8);
    JButton button1 = new JButton("+");
    JTextField field2 = new JTextField(8);
    JButton button2 = new JButton("=");
    JTextField field3 = new JTextField(8);

    public Qiuhe() {
       super("求和工具");
        panel.add(field1);
        panel.add(button1);
        panel.add(field2);
        panel.add(button2);
        panel.add(field3);
        this.add(panel);
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                int x = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText());
                field3.setText(Integer.toString(x));
            }
        });
    }

    public static void main(String[] args) {

        Qiuhe b = new Qiuhe();
        b.setTitle("求和工具");
        b.pack();
        b.setDefaultCloseOperation(EXIT_ON_CLOSE);
        b.setSize(400, 100);
        b.setVisible(true);
    }
}
----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽
2010-10-25 13:10:34
Eline

等 级:论坛游民
帖 子:18
专家分:14
注 册:2010-10-18
  得分:0 
太感谢两位了
----------------解决方案--------------------------------------------------------
3楼的有才!
----------------解决方案--------------------------------------------------------
  相关解决方案