package pplive;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class BS extends JFrame {
static JButton d, e, f, g, h, o, p, i, j, k, l, m, n, q, w, v, x;
static JTextField text1;
public static void main(String[] args) {
BS jf = new BS();
jf.setSize(300, 300);
jf.setTitle("我的JAVA窗体");
jf.addWindowListener(new aa());
Container c = jf.getContentPane();
c.setLayout(new BorderLayout());
JPanel mp1 = new JPanel();
JPanel mp2 = new JPanel();
GridLayout x1 = new GridLayout(2, 1);
GridLayout x2 = new GridLayout(4, 4, 6, 6);
mp1.setLayout(x1);
mp2.setLayout(x2);
text1 = new JTextField();
text1.setSize(300, 100);
mp1.add(text1);
d = new JButton("<html><font size=15 color=red>1");
e = new JButton("<html><font size=15 color=green>2");
f = new JButton("<html><font size=15 color=red>3");
g = new JButton("<html><font size=15 color=red>4");
h = new JButton("<html><font size=15 color=red>5");
o = new JButton("<html><font size=15 color=red>6");
p = new JButton("<html><font size=15 color=red>7");
i = new JButton("<html><font size=15 color=red>8");
j = new JButton("<html><font size=15 color=red>9");
k = new JButton("<html><font size=15 color=blue>0");
l = new JButton("<html><font size=15 color=red>+");
m = new JButton("<html><font size=15 color=red>-");
n = new JButton("<html><font size=15 color=yellow>*");
q = new JButton("<html><font size=15 color=red>/");
w = new JButton("<html><font size=15 color=red>=");
v = new JButton("<html><font size=5 color=red>清零");
x = new JButton("<html><font size=5 color=red>退格");
d.addActionListener(new ab());
e.addActionListener(new ab());
f.addActionListener(new ab());
g.addActionListener(new ab());
h.addActionListener(new ab());
o.addActionListener(new ab());
p.addActionListener(new ab());
i.addActionListener(new ab());
j.addActionListener(new ab());
k.addActionListener(new ab());
l.addActionListener(new ab());
m.addActionListener(new ab());
n.addActionListener(new ab());
q.addActionListener(new ab());
w.addActionListener(new ab());
v.addActionListener(new ab());
x.addActionListener(new ab());
mp2.add(d);
mp2.add(e);
mp2.add(f);
mp2.add(g);
mp2.add(h);
mp2.add(o);
mp2.add(p);
mp2.add(i);
mp2.add(j);
mp2.add(k);
mp2.add(l);
mp2.add(m);
mp2.add(n);
mp2.add(q);
mp2.add(w);
mp2.add(v);
mp2.add(x);
c.add(mp1, BorderLayout.NORTH);
c.add(mp2);
jf.setVisible(true);
}
}
class aa extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class ab implements ActionListener {
public void actionPerformed(ActionEvent e1) {
Object pp = e1.getSource();
int x1 = 0,x2=0,x3=0,x4=0;
String yy;
if (pp == BS.d) {
BS.text1.setText("1");
x1=1;
} if (pp == BS.e) {
BS.text1.setText("2");
x1=2;
}
if (pp == BS.f) {
BS.text1.setText("3");
x1=3;
}
if (pp == BS.g) {
BS.text1.setText("4");
x1=4;
x2=x1;
}
if (pp == BS.h) {
BS.text1.setText("5");
x1=5;
}
if (pp == BS.o) {
BS.text1.setText("6");
x1=6;
}
if (pp == BS.p) {
BS.text1.setText("7");
x1=7;
}
if (pp == BS.i) {
BS.text1.setText("8");
x1=8;
}
if (pp == BS.j) {
BS.text1.setText("9");
x1=9;
}
if (pp == BS.k) {
BS.text1.setText("0");
x1=0;
}
if (pp == BS.l) {
BS.text1.setText("+");
x2+=x1;
}
if (pp == BS.m) {
BS.text1.setText("-");
}
if (pp == BS.n) {
BS.text1.setText("*");
}
if (pp == BS.q) {
BS.text1.setText("/");
}
if (pp == BS.v) {
BS.text1.setText("0");
}
if (pp == BS.w) {
x2+=x1;
BS.text1.setText(""+x1);
}
}
}这段代码,BS.w是按钮“=”,那些数值相加后,还是为零的,怎么改啊,请高手指点下,谢谢了