import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MainFrame extends JFrame implements ActionListener {
boolean clear ;
int a;
int b = 0;
int c = 0;
private JButton btplus;
private JButton btminus;
private JButton btm;
private JButton btdivide;
private JButton btequal;
private JButton btc;
private JButton bt1;
private JButton bt2;
private JButton bt3;
private JButton bt4;
private JButton bt5;
private JButton bt6;
private JButton bt7;
private JButton bt8;
private JButton bt9;
private JButton bt0;
private JTextField text;
public MainFrame() {
super();
initComponents();
}
public MainFrame(String title) {
super(title);
initComponents();
}
private void initComponents() {
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.add(this.getText());
this.add(this.getBtplus());
this.add(this.getBtminus());
this.add(this.getBtm());
this.add(this.getBtdivide());
this.add(this.getBtequal());
this.add(this.getBtc());
this.add(this.getBt0());
this.add(this.getBt1());
this.add(this.getBt2());
this.add(this.getBt3());
this.add(this.getBt4());
this.add(this.getBt5());
this.add(this.getBt6());
this.add(this.getBt7());
this.add(this.getBt8());
this.add(this.getBt9());
}
public JTextField getText() {
if (text == null) {
text = new JTextField();
text.setBounds(40, 30, 200, 30);
}
return text;
}
public JButton getBtplus() {
if (btplus == null) {
btplus = new JButton("+");
btplus.setBounds(90, 200, 50, 30);
btplus.addActionListener(this);
}
return btplus;
}
public JButton getBtminus() {
if (btminus == null) {
btminus = new JButton("-");
btminus.setBounds(140, 200, 50, 30);
btminus.addActionListener(this);
}
return btminus;
}
public JButton getBtm() {
if (btm == null) {
btm = new JButton("*");
btm.setBounds(190, 120, 50, 30);
btm.addActionListener(this);
}
return btm;
}
public JButton getBtequal() {
if (btequal == null) {
btequal = new JButton("=");
btequal.setBounds(190, 160, 50, 30);
btequal.addActionListener(this);
}
return btequal;
}
public JButton getBtdivide() {
if (btdivide == null) {
btdivide = new JButton("/");
btdivide.setBounds(190, 80, 50, 30);
btdivide.addActionListener(this);
}
return btdivide;
}
public JButton getBtc() {
if (btc == null) {
btc = new JButton("c");
btc.setBounds(190, 200, 50, 30);
btc.addActionListener(this);
}
return btc;
}
public JButton getBt1() {
if (bt1 == null) {
bt1 = new JButton("1");
bt1.setBounds(40, 80, 50, 30);
bt1.addActionListener(this);
}
return bt1;
}
public JButton getBt2() {
if (bt2 == null) {
bt2 = new JButton("2");
bt2.setBounds(90, 80, 50, 30);
bt2.addActionListener(this);
}
return bt2;
}
public JButton getBt3() {
if (bt3 == null) {
bt3 = new JButton("3");
bt3.setBounds(140, 80, 50, 30);
bt3.addActionListener(this);
}
return bt3;
}
public JButton getBt4() {
if (bt4 == null) {
bt4 = new JButton("4");
bt4.setBounds(40, 120, 50, 30);
bt4.addActionListener(this);
}
return bt4;
}
public JButton getBt5() {
if (bt5 == null) {
bt5 = new JButton("5");
bt5.setBounds(90, 120, 50, 30);
bt5.addActionListener(this);
}
return bt5;
}
public JButton getBt6() {
if (bt6 == null) {
bt6 = new JButton("6");
bt6.setBounds(140, 120, 50, 30);
bt6.addActionListener(this);
}
return bt6;
}
public JButton getBt7() {
if (bt7 == null) {
bt7 = new JButton("7");
bt7.setBounds(40, 160, 50, 30);
bt7.addActionListener(this);
}
return bt7;
}
public JButton getBt8() {
if (bt8 == null) {
bt8 = new JButton("8");
bt8.setBounds(90, 160, 50, 30);
bt8.addActionListener(this);
}
return bt8;
}
public JButton getBt9() {
if (bt9 == null) {
bt9 = new JButton("9");
bt9.setBounds(140, 160, 50, 30);
bt9.addActionListener(this);
}
return bt9;
}
public JButton getBt0() {
if (bt0 == null) {
bt0 = new JButton("0");
bt0.setBounds(40, 200, 50, 30);
bt0.addActionListener(this);
}
return bt0;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object temp = e.getSource();
if (temp == btc) {
text.setText(null);
clear = true;
}
if (temp == bt0) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "0");
}
if (temp == bt1) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "1");
clear = true;
}
if (temp == bt2) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "2");
clear = true;
}
if (temp == bt3) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "3");
clear = true;
}
if (temp == bt4) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "4");
clear = true;
}
if (temp == bt5) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "5");
clear = true;
}
if (temp == bt6) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "6");
clear = true;
}
if (temp == bt7) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "7");
clear = true;
}
if (temp == bt8) {
if (clear == true)
text.setText(null);
text.setText(text.getText() + "8");
clear = true;
}
if (temp == bt9) {
if (clear == false)
text.setText(null);
text.setText(text.getText() + "9");
clear = true;
}
if (e.getActionCommand().equals("+")) {
c = 1;
try {
clear = false;
a = Integer.parseInt(text.getText());
b += a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
System.out.println("输入错误");
}
}
if (e.getActionCommand().equals("-")) {
c = 2;
try {
clear = false;
a = Integer.parseInt(text.getText());
b -= a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e2) {
System.out.println("输入错误");
}
}
if(e.getActionCommand().equals("*")){
c= 3;
try{
clear = false;
a = Integer.parseInt(text.getText());
b*=a;
text.setText(String.valueOf(b));
}catch(NumberFormatException e3){
System.out.println("输入错误");
}
}
if(e.getActionCommand().equals("/")){
c = 4;
try{
clear = false;
a = Integer.parseInt(text.getText());
b/=a;
text.setText(String.valueOf(b));
}catch(ArithmeticException e4){
System.out.println("除数不能0");
}
}
if(e.getActionCommand().equals("=")){
if(c ==1){
try {
clear = false;
a = Integer.parseInt(text.getText());
b += a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
System.out.println("输入错误");
}
}
else if(c==2){
try {
clear = false;
a = Integer.parseInt(text.getText());
b -= a;
text.setText(String.valueOf(b));
} catch (NumberFormatException e2) {
System.out.println("输入错误");
}
}
else if(c==3){
try{
clear = false;
a = Integer.parseInt(text.getText());
b*=a;
text.setText(String.valueOf(b));
}catch(NumberFormatException e3){
System.out.println("输入错误");
}
}
else if(c==4){
try{
clear = false;
a = Integer.parseInt(text.getText());
b/=a;
text.setText(String.valueOf(b));
}catch(ArithmeticException e4){
System.out.println("除数不能0");
}
}
}
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
MainFrame m = new MainFrame("计算器");
m.setVisible(true);
}
}