1

好的,所以我基本上制作了一个 GUI Java 程序,一个基本的程序。只是加/减/除或乘以文本字段中的数字,没什么大不了的,因为我才开始学习 Java。它可以工作,但有一些错误,例如当您执行它时,如果您单击其中一个单选按钮而不在文本字段中输入数字,则程序将无法运行。当用户单击单选按钮时,如何检查用户是否输入了整数?继承人的代码:

import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
import java.awt.*;


public class GUI extends JFrame{

    Button button1;
    TextField num1;
    TextField num2;
    JRadioButton add,sub,mul,div;
    boolean isnumber = false;


    int x, y, sum;

    public static void main(String[] args){


        new GUI();

    }

    public GUI(){

        thehandler handle = new thehandler();

        JPanel panel = new JPanel();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setSize(800, 70);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Calc");
        this.add(panel);



        button1 = new Button("Calculate");
        button1.addActionListener(handle);
        panel.add(button1);

        num1 = new TextField("Enter a number here");
        num1.addActionListener(handle);
        num2 = new TextField("Enter a number here");
        num2.addActionListener(handle);
        panel.add(num1);
        panel.add(num2);

        add = new JRadioButton("Add");
        add.addActionListener(handle);
        sub = new JRadioButton("Subtract");
        sub.addActionListener(handle);
        mul = new JRadioButton("Multiply");
        mul.addActionListener(handle);
        div = new JRadioButton("Divide");
        div.addActionListener(handle);

        ButtonGroup operation = new ButtonGroup();
        operation.add(add);
        operation.add(sub);
        operation.add(div);
        operation.add(mul);

        panel.add(add);
        panel.add(sub);
        panel.add(mul);
        panel.add(div);




    }

    private class thehandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {


            if(e.getSource() == add){
                sum = x + y;
                x = Integer.parseInt(num1.getText());
                y = Integer.parseInt(num2.getText());           
            }
            if(e.getSource() == sub){
                sum = x - y;
                x = Integer.parseInt(num1.getText());
                y = Integer.parseInt(num2.getText());
            }
            if(e.getSource() == mul){
                sum = x * y;
                x = Integer.parseInt(num1.getText());
                y = Integer.parseInt(num2.getText());
            }
            if(e.getSource() == div){
                sum = x/y;
                x = Integer.parseInt(num1.getText());
                y = Integer.parseInt(num2.getText());
            }
            if(e.getSource() == button1){

                JOptionPane.showMessageDialog(null, "The sum of the desired calculation is... " + sum);
            }

        }

    }



}
4

6 回答 6

4

这取决于整数的含义,但是如果您只想检查字符串是否仅包含数字,并且可以选择-在开始时使用正则表达式来检查它,例如

yourString.matches("-?\\d+");

请注意,这不会检查数字范围。

于 2013-10-19T17:08:08.473 回答
3

只需这样做:

String text = num1.getText();
try {
   Integer x = Integer.parseInt(text);
} catch (NumberFormatException) {
   System.out.println(text + " cannot be converted to integer");
}

如果无法将 String 解析为IntegerNumberFormatException则会抛出 a 。

于 2013-10-19T17:06:12.350 回答
1

您应该检查 TextField 是否为空,并且应该在整数解析中添加一些错误处理,例如:

try {
    x = Integer.parseInt(num1.getText())>
catch (NumberFormatException ex) {
    //error handling
}
于 2013-10-19T17:07:00.953 回答
0

转换为char[]使用toCharArray()

然后循环并检查每个字符是否isDigit()

编辑*正如其他人所说,捕获异常是处理此问题的更好方法

于 2013-10-19T17:06:03.703 回答
0

Integer.parseInt(...)给出它无法解析的东西时,它会抛出一个NumberFormatException. 您应该将parseInt(...)调用包装在 try/catch 块中并适当地处理这种情况:

try {
    x = Integer.parseInt(num1.getText());
} catch (NumberFormatException nfe) {
    // do something about broken data
}

使用单独的方法进行所有的解析和处理会很方便,以减少代码重复量

于 2013-10-19T17:06:33.187 回答
0

你可以使用这样的函数:

boolean isInt(String str) {
     for (int i = 0; i < str.length(); i++) {
         char c = str.charAt(i);
         if (!Character.isDigit(c)) {
            return false;
         }
     }
     return true;
}
于 2013-10-19T17:07:00.070 回答