我无法弄清楚在actionPerformed中try块中的整数量代替什么?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我无法弄清楚在actionPerformed中try块中的整数量代替什么?相关的知识,希望对你有一定的参考价值。

这是一个课程项目,我正在尝试设置一个带有GUI的EF Scale Reporter,它不断出错,告诉我我对字符串输入有一个NumberFormatException。

不介意计算器名称......它只是用作其他东西的模板

此外,它是submitButtonClicker类中的try块,这是我需要帮助的地方,我似乎无法弄清楚我需要做什么来替换整数

    import java.awt.Dimension;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import java.util.InputMismatchException;

        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;
        import javax.swing.JTextField;

        public class CalcButtonFrame extends JFrame{

            private JLabel instructions = new JLabel("Please enter windspeed: ");
            private JTextField firstNumber = new JTextField(5);
            private JButton submit = new JButton("Submit");
            private JButton clear = new JButton("Clear");
            private JLabel lowSpeed = new JLabel("65");
            private JLabel highSpeed = new JLabel("200");

            CalcDrawing drawing = new CalcDrawing();

            public CalcButtonFrame() {

                JPanel panel = new JPanel();
                panel.add(instructions);
                panel.add(firstNumber);
                panel.add(submit);
                panel.add(clear);
                panel.add(drawing);

                drawing.setPreferredSize(new Dimension(200, 60));

                ClearButtonListener cbl = new ClearButtonListener();
                clear.addActionListener(cbl);

                SubmitButtonListener sbl = new SubmitButtonListener();
                submit.addActionListener(sbl);
                add(panel);

            }


        class ClearButtonListener implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                firstNumber.setText("");

                Calculator empty = new Calculator();
                drawing.adjustFill(empty.determinePercentage());

            }

        }

        class SubmitButtonListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                try {
                    Integer amount = Integer.parseInt(firstNumber.getText());
                    firstNumber.setText(String.valueOf(amount));        


                    Integer high = Integer.parseInt(highSpeed.getText());
                    highSpeed.setText(String.valueOf(high));        

                    Calculator user = new Calculator(amount, high);

                    //after creating the percentage method
                    drawing.adjustFill(user.determinePercentage());

                }
                catch(NumberFormatException Exception) {
                    String a = firstNumber.getText();
                    int amount = Integer.parseInt(a);

                    String l = lowSpeed.getText();
                    double low = Double.parseDouble(l);

                    if(amount < low){

                        throw new IllegalArgumentException("Must be greater than 65");
                    }

                }

                try {       
                    Integer amount = Integer.parseInt(firstNumber.getText());
                    firstNumber.setText(String.valueOf(amount));        

                    Integer high = Integer.parseInt(highSpeed.getText());
                    highSpeed.setText(String.valueOf(high));

                    Calculator user = new Calculator(amount, high);

                    //after creating the percentage method
                    drawing.adjustFill(user.determinePercentage());

                }
                catch(NumberFormatException e1) {
                        JPanel panel = new JPanel();
                        JLabel enter = new JLabel("Please enter a number");
                        panel.add(enter);
                        System.out.println("not working");
                }



                }
        }
        }

    import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class CalcDrawing extends JComponent{

    private int x= 10;
    private int y = 10;
    private int totalLength = 170;
    private int height = 30;
    //create another variable after adjustFill
    private int fillForFirstSection = 170;

    //rectangle 
    public void paintComponent(Graphics g) {

        g.setColor(Color.black);
        g.fillRect(x, y, totalLength, height);

        g.setColor(Color.white);
        g.fillRect(x, y, fillForFirstSection, height);

    }

    public void adjustFill(double fillAmount) {
        // TODO Auto-generated method stub

        double fill = totalLength * fillAmount;
        fillForFirstSection = (int)fill;
        repaint();

    }
}

    import javax.swing.JFrame;

public class StartCalc {

    public static void main(String[] args) {
        //TODO Auto-generated method stub

            JFrame frame = new CalcButtonFrame();
            frame.setSize(400, 200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    }


}




public class Calculator {
    private double amount;
    private int maxMPH = 200;

    public Calculator() {
        //TODO Auto-generated constructor stub

    }

    public Calculator(double amount, int maxMPH) {
        this.amount = amount;
        this.maxMPH = maxMPH;
    }

    //getters and setters

    public double getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    //new method
    public double determinePercentage() {
        return (int)this.amount/(int)this.maxMPH;
    }


}
答案

看着...

try {
    Integer amount = Integer.parseInt(firstNumber.getText());
    //...
} catch (NumberFormatException Exception) {
    String a = firstNumber.getText();
    int amount = Integer.parseInt(a);
    //...
}

如果Integer amount = Integer.parseInt(firstNumber.getText());失败,那么在int amount = Integer.parseInt(a);区块尝试catch是没有意义的,因为它已经失败了。

以上是关于我无法弄清楚在actionPerformed中try块中的整数量代替什么?的主要内容,如果未能解决你的问题,请参考以下文章

PJSIP iOS 错误

在Apache Spark中使用Bigquery Connector时如何设置分区数?

jpopupmenu menuitem actionperformed未触发

Angular - 调度动作时出现“TypeError:无法冻结”

在while循环中输入Cin

Visual Studio日志文件