package calcu;
import java.awt.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.*;
import java.awt.event.*;
public class calcu extends JFrame implements ActionListener,ItemListener{
JLabel bq1,bq2,bq3;
int index;
JTextField wbk1,wbk2,wbk3;
JButton an;
JComboBox xlk;
public static void main(String[] args) {
// TODO Auto-generated method stub
calcu a=new calcu();
}
public calcu(){
bq1=new JLabel("操作数1:");
bq2=new JLabel("操作数2:");
bq3=new JLabel("计算类型: ");
wbk1=new JTextField(10);
wbk2=new JTextField(10);
wbk3=new JTextField(10);
String[] b={"+","-","*","/"};
xlk=new JComboBox(b);
an=new JButton("计算");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
this.add(bq1);
this.add(bq2);
this.add(bq3);
this.add(an);
this.add(wbk1);
this.add(wbk2);
this.add(wbk3);
this.add(xlk);
an.addActionListener(this);
an.setActionCommand("1");
xlk.addItemListener(this);
wbk1.addActionListener(this);
wbk2.addActionListener(this);
wbk3.addActionListener(this);
GridBagConstraints s= new GridBagConstraints();
s.fill = GridBagConstraints.BOTH;
s.insets=new Insets(5,5,5,5);
s.gridx=0;
s.gridy=0;
layout.setConstraints(bq1,s);
s.gridx=0;
s.gridy=1;
layout.setConstraints(bq2,s);
s.gridx=0;
s.gridy=2;
layout.setConstraints(bq3,s);
s.gridx=0;
s.gridy=3;
layout.setConstraints(an,s);
s.gridx=1;
s.gridy=0;
layout.setConstraints(wbk1,s);
s.gridx=1;
s.gridy=1;
layout.setConstraints(wbk2,s);
s.gridx=1;
s.gridy=2;
layout.setConstraints(xlk,s);
s.gridx=1;
s.gridy=3;
layout.setConstraints(wbk3, s);
this.setTitle("计算器");
this.setSize(300,210);
this.setLocation(500, 250);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String word=wbk1.getText();
int i=Integer.parseInt(word);
String wor=wbk2.getText();
int j=Integer.parseInt(wor);
int f=0;
switch(this.index){
case 0:
f=i+j;
break;
case 1:
f=i-j;
break;
case 2:
f=i*j;
break;
case 3:
f=i/j;
break;
}
String t= String.valueOf(f);
if(e.getActionCommand().equals("1")){
wbk3.setText(t);
}
}
public void itemStateChanged(ItemEvent a){
index=xlk.getSelectedIndex();
}
}