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

时间:2019-04-30 00:01:20

标签: java jframe

这是一个课程项目,我正在尝试使用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;
    }


}

1 个答案:

答案 0 :(得分:0)

看着...

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毫无意义,因为它已经失败了。