gpt4 book ai didi

java - 无法从 JTextField 获取整数

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

我试图从 JTextField 获取整数,但我不断收到 NumberFormatException。我使用了下面的代码:

JTextField price = new JTextField();
price.addActionListener(new ComboListener());
String inputText = price.getText();
int inputPrice = Integer.parseInt(inputText);

每个网站都说这是正确的方法,所以我不明白我做错了什么。

编辑:完整代码在这里:

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

public class RatePanel extends JPanel {

private double[] rate; // exchange rates
private String[] currencyName;
private JLabel result;

public RatePanel() {
currencyName = new String[]{"Select the currency..",
"European Euro", "Canadian Dollar",
"Japanese Yen", "Australian Dollar",
"Indian Rupee", "Mexican Peso"};

rate = new double[]{0.0, 1.2103, 0.7351,
0.0091, 0.6969, 0.0222, 0.0880};

JLabel title = new JLabel("How much is that in dollars?");
title.setAlignmentX(Component.CENTER_ALIGNMENT);
title.setFont(new Font("Helvetica", Font.BOLD, 20));
add(title);
add(Box.createRigidArea(new Dimension(0, 100)));
JLabel enter = new JLabel("Enter cost of item");
enter.setAlignmentX(Component.LEFT_ALIGNMENT);
enter.setFont(new Font("Helvetica", Font.BOLD, 20));
add(enter);
JTextField price = new JTextField();
price.addActionListener(new BoxListener());
price.setAlignmentX(Component.RIGHT_ALIGNMENT);
add(price);
add(Box.createRigidArea(new Dimension(0, 100)));
JLabel select = new JLabel("Select a currency: ");
select.setAlignmentX(Component.LEFT_ALIGNMENT);
select.setFont(new Font("Helvetica", Font.BOLD, 20));
add(select);
JComboBox Cbox = new JComboBox(currencyName);
Cbox.addActionListener(new ComboListener());
Cbox.setAlignmentX(Component.RIGHT_ALIGNMENT);
add(Cbox);
String index = Cbox.getSelectedItem().toString();
}

public class BoxListener implements ActionListener {

public void actionPerformed(ActionEvent event) {
String inputText = price.getText();
int inputPrice = Integer.parseInt(inputText);
}
}

public class ComboListener implements ActionListener {

public void actionPerformed(ActionEvent event, String index, double inputPrice, double[] rate) {
double finalPrice = 0;
switch (index) {
case "European Euro":
finalPrice = inputPrice * rate[1];
break;
case "Canadian Dollar":
finalPrice = inputPrice * rate[2];
break;
case "Japanese Yen":
finalPrice = inputPrice * rate[3];
break;
case "Australian Dollar":
finalPrice = inputPrice * rate[4];
break;
case "Indian Rupee":
finalPrice = inputPrice * rate[5];
break;
case "Mexican Peso":
finalPrice = inputPrice * rate[6];
break;
}

result = new JLabel(inputPrice + "USD equals " + finalPrice
+ index);
add(result);
}

@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}

最佳答案

我运行了你的代码,它实际上工作得很好(没有给我 NumberFormatException)。

您收到 NumberFormatException 可能是因为您尝试了以下操作:

  1. 当文本字段为空时按 Enter
  2. 当文本字段包含非数字输入时,在文本字段上按 Enter

在尝试将文本字段的内容解析为整数之前,您可以向输入添加验证:

public class BoxListener implements ActionListener {

public void actionPerformed(ActionEvent event) {
String s= price.getText();
if(s.matches("[0-9]+")) //Perform validation before parsing string
inputPrice = Integer.parseInt(s);
}
}

另请注意,我没有将 inputPrice 和其他组件(例如文本字段)声明为局部变量,而是将它们声明为 RatePanel 的实例变量。

<小时/>

将变量声明为实例变量而不是局部变量:

class RatePanel extends JPanel{
private JTextfield txtPrice; // <--- declare here as instance variable
private int inputPrice; // <--- declare here as instance variable

public RatePanel(){
//If you declare in the constructor or other methods, they become local variables.
}
}

关于java - 无法从 JTextField 获取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46085582/

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