gpt4 book ai didi

java - 我不知道用什么来代替 actionPerformed 中 try block 中的整数?

转载 作者:行者123 更新时间:2023-12-02 06:10:19 25 4
gpt4 key购买 nike

这是一个类(class)项目,我正在尝试使用 GUI 设置 EF Scale Reporter,但它不断出错,告诉我字符串输入出现 NumberFormatException。

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

此外,这是submitButtonClicker类中的try block ,这是我需要帮助的地方,我似乎不知道我需要做什么来代替整数金额

    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 block 中,因为它已经失败了。

关于java - 我不知道用什么来代替 actionPerformed 中 try block 中的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com