//Double(String)构造函数可能会抛出异常,捕获它就可以了
try{
Double dbl1=new Double(input1TextField.getText());
Double dbl2=new Double(input2TextField.getText());
}catch(NumberFormatException){
//弹出对话框
return;
}
double d1 = dbl1.doubleValue();
double d2 = dbl2.doubleValue();
...
try{
Double dbl1=new Double(input1TextField.getText());
Double dbl2=new Double(input2TextField.getText());
}catch(NumberFormatException){
//弹出对话框
return;
}
double d1 = dbl1.doubleValue();
double d2 = dbl2.doubleValue();
...
解决方案 »
- 怎样在main函数种调用类成员函数
- mysql中如何动态创建表?
- 关于Microsoft SQL Server 2000 Driver for JDBC驱动的问题
- 掉用包的。(比较基础,大家一起看看。给点意介)
- 怎样在web环境下使用jni?
- 怎么取DOUBLE型变量的值只到小数点后一位啊!
- 我在运行一个简单的java程序时,总是出现这个问题
- 怎么实现else判断结束返回上一个if语句???
- 问题:如何使用Java取得系统JAVA_HOME的设置和classpath路径信息
- 哪位大虾有together5.5的key?
- 为什么构造函数不用加返回类型void,而普通函数如果不需要返回值也必须加void?
- 怎么会这样了!dereference是什么错误???
try{
Double dbl1=new Double(input1TextField.getText());
Double dbl2=new Double(input2TextField.getText());
}catch(NumberFormatException nfe){
//弹出对话框
return;
}
double d1 = dbl1.doubleValue();
double d2 = dbl2.doubleValue();
...
Double dbl1,dbl2;
try{
dbl1=new Double(input1TextField.getText());
dbl2=new Double(input2TextField.getText());
}catch(NumberFormatException nfe){
//弹出对话框,do something
return;
}
double d1 = dbl1.doubleValue();
double d2 = dbl2.doubleValue();
...
double d1 =
new Double(input1TextField.getText()).doubleValue();
double d2 =
new Double(input2TextField.getText()).doubleValue();
}catch(NumberFormatException nfe){
//do something
return ;
}
catch(NumberFormatException nfe){
System.out.println(input1TextField.getText()+"or"+input2TextField.getText()+"is wrong number");
return;
}
... ...调试可以通过,但
为什么出现这样的问题:当我只要输入非字符时,显示的都是input1TextField.getText()的内容!如:我输入的是2与a时:显示的是2or2 is wrong number;
输入的是a与2时:显示的是aora is wrong number;
不理解?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Calculator4 extends JFrame { // Components are treated as attributes, so that they will be
// visible to all of the methods of the class. private Container contentPane; // Use descriptive names for components where possible; it makes
// your job easier later on! private JPanel leftPanel;
private JPanel centerPanel;
private JPanel buttonPanel;
private JTextField input1TextField;
private JTextField input2TextField;
private JLabel answerLabel;
private JButton plusButton;
private JButton minusButton; // Constructor.
public Calculator4() {
// Invoke the generic JFrame constructor.
super("Simple Calculator"); // The content pane container is now declared to be an
// attribute. Note that the use of "this." is unnecessary;
// we could have simply written:
// contentPane = getContentPane();
contentPane = this.getContentPane();
this.setSize(250, 100); // Technique for centering a frame on the screen.
Dimension frameSize = this.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2); leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(3, 1));
leftPanel.add(new JLabel("Input 1: "));
leftPanel.add(new JLabel("Input 2: "));
leftPanel.add(new JLabel("Answer: "));
contentPane.add(leftPanel, BorderLayout.WEST); centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 1));
input1TextField = new JTextField(10);
input2TextField = new JTextField(10);
answerLabel = new JLabel();
centerPanel.add(input1TextField); centerPanel.add(input2TextField);
centerPanel.add(answerLabel);
contentPane.add(centerPanel, BorderLayout.CENTER); buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2, 1));
plusButton = new JButton("+");
minusButton = new JButton("-");
buttonPanel.add(plusButton);
buttonPanel.add(minusButton);
contentPane.add(buttonPanel, BorderLayout.EAST); // Add behaviors! This time we use the SAME
// listener object to listen to BOTH buttons. ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Double dbl1,dbl2;
try{
dbl1=new Double(input1TextField.getText());
dbl2=new Double(input2TextField.getText());
}catch(NumberFormatException nfe){
//弹出对话框,do something
System.out.println(input1TextField.getText()+"or"+input2TextField.getText()+" is wrong number");
return;
}
double d1 = dbl1.doubleValue();
double d2 = dbl2.doubleValue();
if (e.getSource() == plusButton)
answerLabel.setText("" + (d1 + d2));
else answerLabel.setText("" + (d1 - d2));
}
}; plusButton.addActionListener(l);
minusButton.addActionListener(l); this.setVisible(true);
} public static void main(String[] args) {
Calculator4 c = new Calculator4();
c.pack();
c.show();
}
}