JAVA做的计算器出了个问题求助
我用for循环定义了按键数组并且同样用循环设置了按钮。可是监听器却似乎没有效果,运行程序的时候点击按钮没反应……程序代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JiSuanQi extends JFrame implements ActionListener { String s = "", s1 = null, s2 = null; JFrame f = new JFrame("计算器"); JTextField xianshi = new JTextField(30); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JButton dengyu = new JButton("="); JButton qiuyu = new JButton("%"); JButton del = new JButton("Del"); JButton[] bt = new JButton[16]; int id = 0; public static void main(String[] args) { new JiSuanQi().jisuan(); } public void jisuan() { f.setLayout(new BorderLayout(8, 8)); p2.setLayout(new GridLayout(4, 4, 8, 8)); p3.setLayout(new BorderLayout(8, 8)); f.add(p1, BorderLayout.NORTH); f.add(p2); p3.add(del, BorderLayout.NORTH); p3.add(qiuyu, BorderLayout.SOUTH); p3.add(dengyu, BorderLayout.CENTER); p1.add(xianshi); f.add(p3, BorderLayout.EAST); String[] b = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "C", "/" }; // 设置颜色 for (int i = 0; i < 16; i++) { bt[i] = new JButton(b[i]); p2.add(bt[i]); } for (int i = 0; i < 16; i++) { if (i == 3 | i == 7 | i == 11 | i == 15 | i == 14) { bt[i].setForeground(Color.BLACK); } else { bt[i].setForeground(Color.BLUE); } } dengyu.setForeground(Color.BLACK); del.setForeground(Color.BLACK); f.pack(); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); for (int i = 0; i < b.length; i++) { (new Button(b[i])).addActionListener(this); } } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(e.getSource() == bt[0]){ s += 1; s2 += 1; xianshi.setText(s); } if(e.getSource() == bt[1]){ s += 2; s2 += 2; xianshi.setText(s); } if(e.getSource() == bt[2]){ s += 3; s2 += 3; xianshi.setText(s); } if(e.getSource() == bt[3]){ s1 = s; s += '+'; id = 1; s2 = ""; xianshi.setText(s); } if(e.getSource() == bt[4]){ s += 4; s2 += 4; xianshi.setText(s); } if(e.getSource() == bt[5]){ s += 5; s2 += 5; xianshi.setText(s); } if(e.getSource() == bt[6]){ s += 6; s2 += 6; xianshi.setText(s); } if(e.getSource() == bt[7]){ s1 = s; s += '-'; id = 2; s2 = ""; xianshi.setText(s); } if(e.getSource() == bt[8]){ s += 7; s2 += 7; xianshi.setText(s); } if(e.getSource() == bt[9]){ s += 8; s2 += 8; xianshi.setText(s); } if(e.getSource() == bt[10]){ s += 9; s2 += 9; xianshi.setText(s); } if(e.getSource() == bt[11]){ s1 = s; s += '*'; id = 3; s2 = ""; xianshi.setText(s); } if(e.getSource() == bt[12]){ s += 0; s2 += 0; xianshi.setText(s); } if(e.getSource() == bt[13]){ s += '.'; s2 += '.'; xianshi.setText(s); } if(e.getSource() == bt[14]){ s = ""; s2 = ""; xianshi.setText(s); } if(e.getSource() == bt[15]){ s1 = s; s += '/'; id = 4; s2 = ""; xianshi.setText(s); } if(e.getSource() == dengyu){ if (id < 1) ; else { s += '='; double a = Double.parseDouble(s1); double b = Double.parseDouble(s2); double c = 0; switch (id) { case 1: c = a + b; break; case 2: c = a - b; break; case 3: c = a * b; break; case 4: c = a / b; break; } s += c; xianshi.setText(s); } s = ""; s1 = ""; s2 = ""; id = 0; } if(e.getSource() == del){ char[] c1; char[] c2 = new char[s.length() - 1]; c1 = s.toCharArray(); for (int i = 0; i < s.length() - 1; i++) c2[i] = c1[i]; s = s.valueOf(c2); if (id < 1) { s1 = s; } if (s2.length() >= 1) { char[] c3; char[] c4 = new char[s2.length() - 1]; c3 = s2.toCharArray(); for (int i = 0; i < s2.length() - 1; i++) c4[i] = c3[i]; s2 = s2.valueOf(c4); } xianshi.setText(s); } } }