Java 将.getText()与Integer.parseInt()一起使用时出错
因此,我试图制作一个程序,当一个整数,即5,被输入到文本区域时,点击三个按钮中的一个,运行一个从文本区域获取整数的方法,并将其放入for循环。我现在不关心这些方法,我只是想让我的程序运行起来Java 将.getText()与Integer.parseInt()一起使用时出错,java,Java,因此,我试图制作一个程序,当一个整数,即5,被输入到文本区域时,点击三个按钮中的一个,运行一个从文本区域获取整数的方法,并将其放入for循环。我现在不关心这些方法,我只是想让我的程序运行起来 import java.awt.event.*; import javax.swing.*; public class Main { static JTextField textfield = new JTextField("Enter the size of your shape: "); stati
import java.awt.event.*;
import javax.swing.*;
public class Main {
static JTextField textfield = new JTextField("Enter the size of your shape: ");
static String size = textfield.getText();
static int w = Integer.parseInt(size);
static JTextArea textarea = new JTextArea(6, 37) ;
public static void main(String[] args) {
JFrame f = new JFrame();// creating instance of JFrame
JButton bSquare = new JButton("Square");// creating instance of JButton
JButton bRATriangle = new JButton("Right Angle Triangle");// creating instance of JButton
JButton bETriangle = new JButton("Equilateral Triangle");// creating instance of JButton
bSquare.setBounds(50, 100, 100, 40);// x axis, y axis, width, height
bRATriangle.setBounds(250, 100, 100, 40);// x axis, y axis, width, height
bETriangle.setBounds(450, 100, 100, 40);// x axis, y axis, width, height
textfield.setBounds(200, 200, 200, 80);
bSquare.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i > w; i++) {
textfield.setText("YEAH BOIIIIIII!");
}
}
});
当我尝试运行该程序时,会出现以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: For input string: "Enter the size of your shape: "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Main.<clinit>(Main.java:8)
线程“main”java.lang.ExceptionInInitializeError中的异常
原因:java.lang.NumberFormatException:对于输入字符串:“输入形状的大小:” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:580)处 在java.lang.Integer.parseInt(Integer.java:615) 在Main(Main.java:8) 有没有办法解决这个问题? 提前感谢。所有这些都是静态的
static JTextField textfield = new JTextField("Enter the size of your shape: ");
static String size = textfield.getText();
static int w = Integer.parseInt(size);
static JTextArea textarea = new JTextArea(6, 37) ;
因此,它们是在类加载后执行的,不会给您提供输入的机会
解决方案是:不要使用静态对象,此JTextField文本字段应为空,仅用于数字输入您在字段声明中解析
字符串
,将其转换为整数
:
static String size = textfield.getText();
int w = Integer.parseInt(size);
这毫无意义。您应该在
textfield
字段中添加一个侦听器,在这个侦听器中,您当然可以通过try/catch来执行此转换。由于您不确定输入是否为
整数
,因此应处理异常情况。例如:
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
w = Integer.parseInt(textfield.getText());
}
catch(NumberFormatException nfe){
... // handle the exception
}
}
}
“输入形状的大小:”不是数字。您希望前3个变量做什么?显然,一场崩溃正在发生。您应该将文本字段添加到视图中,添加一个按钮或onChange侦听器,然后才
尝试解析文本。您使用文本“输入形状大小:”创建文本字段,然后立即获取该文本并尝试将其转换为数字。公平地说,这显然是一种方法。只需在侦听器中执行代码即可。您应该了解,您的程序不会自动将getText的值绑定到您的变量。您必须编写代码,以便在运行时获取文本值。在您的情况下,您应该在actionperformed方法中编写代码。请注意:加载的类与实例化的类不同。尽管删除了静态对象并将textfield留空,但我仍然会收到相同的错误@ΦXocę웃 Пepeúpaツ