import java.awt.*;
import java.awt.event.*;
import javax.swing;
public class Calculator extends JPanel{
static JFrame frame;
static JPanel subPanel;
static String add="+";
static String subtract="-";
static String multiply="*";
static String divide="/";
JTextField num1TextField,num2TextField,resultTextField;
JButton equalButton;
ButtonGroup group;
JradioButton addButton,subtractButton,multiplyButton,divideButton;
public Calculator(){
num1TextField=new JTextField(5);
num2TextField=new JTextField(5);
resultTextField=new JTextField(5);
equalButton=new JButton("=");
addButton=new JRadioButton(add);
addButton.setActionCommand(add);
subtractButton=new JRadionButton(subtract);
subtractButton.setActionCommand(subtract);
multiplyButton=new JRadionButton(multiply);
multiplyButton.setActionCommand(multiply);
divideButton=new JRadionButton(divide);
divideButton.setActionCommand(divide);
group =new ButtonGroup();
group.add(addButton);
group.add(subtractButton);
group.add(multiplyButton);
group.add(divideButton);
addButton.setElected(true);
subPanel=new Jpanel();
subPanel.setLayout(new GridLayout(4,1));
subPanel.add(addButton);
subPanel.add(subtractButton);
subPanel.add(multiplyButton);
subPanel.add(divideButton);
add(num1TextField);
add(subPanel);
add(num1TextField);
add(equalButton);
add(resultTextField);
ButtonListener bnListener=new ButtonListener();
equalButton.addActionListener(bnListener);
}
public static void main(String s[]){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception exc){
System.err.println("Error loadingL&F:"+exc);
}
Calculator panel=new Calculator();
frame =new JFrame("Calculator");
frame.addWindowListener(new WindowAdapter()){
public void windowClosing(windowEvent e){System.exit(0);}
}
frame.getContentPane().add("Center",panel);
frame.pack();
frame.setVisible(true);
}
}
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Double n1=new Double(num1TextField.getText());
Double n2=new Double(num2TextField.getText());
double num1,num2,num3;
num1=n1.doubleValue();
num2=n2.doubleValue();
String cmd=group.getSelected().getActionCommand();
if(cmd==add)
num3=num1+num2;
else if(cmd==subtract)
num3=num1-num2;
else if(cmd==multiply)
num3=num1*num2;
else
num3=num1/num2;
Double n3=new Double(num3);
resultTextField.setText(n3.toString());
resultTextField.select(0,0)
}
}
----------------解决方案--------------------------------------------------------
你错误超多的 自己慢慢改去吧
----------------解决方案--------------------------------------------------------