好的,所以我基本上制作了一个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);
            }

        }

    }



}

最佳答案

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

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


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

07-25 22:10
查看更多