import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
class sss implements ActionListener{
JTextField text1,text2,text3;
JButton button;
ActionListener bl;
JComboBox box;
double number1,number2;
sss(){
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
// 等价于frame.getContentPane().setLayout(new FlowLayout());
text1 = new JTextField(8);
text2 = new JTextField(8);
text3 = new JTextField(8);
button = new JButton("=");
String[] arr = new String[] {"+","-","*","/"} ;
box = new JComboBox();
for(int i = 0; i<arr.length;i++) {
box.addItem(arr[i]);
}
button.addActionListener(this);
text1.addActionListener(this);
text2.addActionListener(this);
text3.addActionListener(this);
frame.add(text1);
frame.add(box);
frame.add(text2);
frame.add(button);
frame.add(text3);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new sss();
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1 = text1.getText();
number1 = Double.valueOf(str1);
String str2 = text2.getText();
number2 = Double.valueOf(str2);
String fuhao = (String) box.getSelectedItem();
if(e.getSource().equals(button)){
if(fuhao == "+") {
double result = number1 + number2;
text3.setText(""+result);
}
if(fuhao == "-") {
double result = number1 - number2;
text3.setText(""+result);
}
if(fuhao == "*") {
double result = number1 * number2;
text3.setText(""+result);
}
if(fuhao == "/") {
double result = number1 / number2;
text3.setText(""+result);
}
}
}
}