计算器源代码有兴趣的可以来看看
package calculator;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 计算机代码
*
* @author xuwei 2013-5-20 11:39:43
*/
class MianFrame implements ActionListener {
JFrame jf = new JFrame("计算器");
JPanel jp = new JPanel(new BorderLayout());
JPanel jp1 = new JPanel(new GridLayout(4, 4));
JTextField jt = new JTextField("");
JButton button[] = new JButton[10];//按钮的数组
JButton bjia = new JButton("+");
JButton bjian = new JButton("-");
JButton bcheng = new JButton("*");
JButton bchu = new JButton("/");
JButton bdeng = new JButton("=");
JButton mc = new JButton("MC");
private String number[] = new String[10];
intnumber0;
intnumber1;
String fuhao;
publicvoid frame1() {
jf.add(jp);
jp.add(jt, BorderLayout.NORTH);
jp.add(jp1, BorderLayout.CENTER);
jt.setHorizontalAlignment(JTextField.RIGHT);
jt.setEditable(false);
for (int i = 0; i < 10; i++) {
button[i] = new JButton(String.valueOf(i));
}
jp1.add(button[1]);
jp1.add(button[2]);
jp1.add(button[3]);
jp1.add(bjia);
jp1.add(button[4]);
jp1.add(button[5]);
jp1.add(button[6]);
jp1.add(bjian);
jp1.add(button[7]);
jp1.add(button[8]);
jp1.add(button[9]);
jp1.add(bcheng);
jp1.add(mc);
jp1.add(button[0]);
jp1.add(bdeng);
jp1.add(bchu);
for (int i = 0; i < 10; i++) {
button[i].addActionListener(this);
}
mc.addActionListener(this);
bjia.addActionListener(this);
bjian.addActionListener(this);
bcheng.addActionListener(this);
bchu.addActionListener(this);
bdeng.addActionListener(this);
jf.setSize(300, 200);
jf.setVisible(true);
}
publicvoid actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == button[i]) {
jt.setText(jt.getText() + i);
}
}
if (e.getSource() == bjia) {
number0 = Integer.valueOf(jt.getText());
fuhao = bjia.getText();
jt.setText("");
} elseif (e.getSource() == bjian) {
number0 = Integer.valueOf(jt.getText());
fuhao = bjian.getText();
jt.setText("");
} elseif (e.getSource() == bcheng) {
number0 = Integer.valueOf(jt.getText());
fuhao = bcheng.getText();
jt.setText("");
} elseif (e.getSource() == bchu) {
number0 = Integer.valueOf(jt.getText());
fuhao = bchu.getText();
jt.setText("");
} elseif (e.getSource() == bdeng) {
number1 = Integer.valueOf(jt.getText());
if ("+".equals(fuhao)) {
jt.setText(String.valueOf(number0 + number1));
} elseif ("-".equals(fuhao)) {
jt.setText(String.valueOf(number0 - number1));
} elseif ("*".equals(fuhao)) {
jt.setText(String.valueOf(number0 * number1));
} elseif ("/".equals(fuhao)) {
jt.setText(String.valueOf(number0 / number1));
}
} elseif (e.getSource() == mc) {
jt.setText(null);
}
}
/**
* @return the jt
*/
public JTextField getJt() {
returnjt;
}
/**
* @param jt
* the jt to set
*/
publicvoid setJt(JTextField jt) {
this.jt = jt;
}
}
publicclass Calculator {
publicstaticvoid main(String[] args) {
MianFrame mf = new MianFrame();
mf.frame1();
}
}