这段java程序哪里错了?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Fwl {
public static void main(String[] args) {
new MyFrame("计算器",1920/2,1080/2,320,532);
}
}
class MyFrame extends JFrame {
JTextField t;
MyFrame(String s,int x,int y,int w,int h) {
super(s);
setBounds(x,y,w,h);
JPanel p1 = new JPanel();
t = new JTextField(20);
t.setSize(20,20);
p1.add(t);
JPanel p2 = new JPanel();
p2.setSize(300,500);
p2.setLayout(new GridLayout(4,4));
String z = "1,2,3,+,4,5,6,-,7,8,9,*,0,.,/,=";
String[] f = z.split(",");
for(int i=0;i<f.length;i++) {
JButton b = new JButton(f[i]);
b.setSize(30,30);
b.addActionListener(new MyEvent(this));
b.setActionCommand(f[i]);
p2.add(b);
}
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyEvent implements ActionListener {
int x,y,s,j,c,u;
MyFrame a;
MyEvent(MyFrame a) {
this.a = (MyFrame)a;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("+")) {
x = Integer.parseInt(a.t.getText());
a.t.setText(a.t.getText()+e.getActionCommand());
s = 1;
}
else if(e.getActionCommand().equals("-")) {
x = Integer.parseInt(a.t.getText());
a.t.setText(a.t.getText()+e.getActionCommand());
j = 1;
}
else if(e.getActionCommand().equals("*")) {
x = Integer.parseInt(a.t.getText());
a.t.setText(a.t.getText()+e.getActionCommand());
c = 1;
}
else if(e.getActionCommand().equals("/")) {
x = Integer.parseInt(a.t.getText());
a.t.setText(a.t.getText()+e.getActionCommand());
u = 1;
}
else if(e.getActionCommand().equals("=")) {
y = Integer.parseInt(a.t.getText());
if(s==1) {
int sum = x+y;
System.out.print(x);
}
}
else {
a.t.setText(a.t.getText()+e.getActionCommand());
}
}
}
你程序的设计方法完全不对,就拿你的加法来说
(程序76行)
y = Integer.parseInt(a.t.getText());
你若输入
1+2
那么
a.t.getText()的值为字串的"1+2"
它不能通过 Integer.parseInt变成整形的
且你的
int sum = x+y; 中的x又是什么?