用 java 写一个计算器 求帮忙把代码补全 需补处有注释
import java.awt.FlowLayout;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
public class GUI_Calculator extends JFrame{
//Define the components we need to use
private JTextField txtNum1,txtNum2,txtResult;
private JButton btnCal;
private JComboBox cmb_operater;
@SuppressWarnings("rawtypes")
public GUI_Calculator()
{
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setSize(250, 180);
//Initialize the Frame Information
txtNum1 = new JTextField();
this.txtNum1.setColumns(8);
cmb_operater = new JComboBox();
this.cmb_operater.addItem("+");
this.cmb_operater.addItem("-");
this.cmb_operater.addItem("*");
this.cmb_operater.addItem("/");
//add Event handler to the "cmb_operater" to make it response to the selection.
//Here needs the code
//end of the code
txtNum2 = new JTextField();
txtNum2.setColumns(8);
txtResult = new JTextField();
txtResult.setColumns(8);
txtResult.setEnabled(false);
this.btnCal = new JButton("OK");
this.btnCal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
double num1,num2,result=0;
char operator;
if(e.getSource()==btnCal)
{
num1 = Double.parseDouble(txtNum1.getText());
num2 = Double.parseDouble(txtNum2.getText());
//get the operator,needs code Here
operator = **********************;
switch(operator)
{
case '+':result = num1+num2;break;
case '-':result = num1-num2;break;
case '*':result = num1*num2;break;
case '/':result = num1/num2;break;
}
txtResult.setText(String.valueOf(result));
}
}
});
this.add(txtNum1);
this.add(cmb_operater);
this.add(txtNum2);
this.add(new JLabel(" = "));
this.add(txtResult);
this.add(btnCal);
this.setVisible(true);
}
public static void main(String[] args){
GUI_Calculator gc = new GUI_Calculator();
}
}