当前位置: 代码迷 >> Java相关 >> [求助]看一上我的代码问题出在哪里?
  详细解决方案

[求助]看一上我的代码问题出在哪里?

热度:231   发布时间:2006-10-01 07:24:36.0
[求助]看一上我的代码问题出在哪里?

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)


}


}

搜索更多相关的解决方案: 代码  

----------------解决方案--------------------------------------------------------

你错误超多的 自己慢慢改去吧


----------------解决方案--------------------------------------------------------
  相关解决方案