| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 376 人关注过本帖
标题:[求助]看一上我的代码问题出在哪里?
取消只看楼主 加入收藏
风雨中
Rank: 1
等 级:新手上路
帖 子:213
专家分:0
注 册:2005-10-6
收藏
 问题点数:0 回复次数: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)


}


}

搜索更多相关主题的帖子: 代码 
2006-10-01 07:24
快速回复:[求助]看一上我的代码问题出在哪里?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020948 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved