求纠正一下程序
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);
}
}程序没有错,但是为什么运行是结果出现的文本框不是计算器那样的呢?
----------------解决方案--------------------------------------------------------
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);
}
}
----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽