Java 文本字段中出现负数错误
这是我第一次在这里问你,如果你需要比我提供的更多的信息,尽管问我 我已经学习Java几个星期了,最后我决定尝试制作自己的程序,这是第一个,所以代码会很混乱。它基本上是一个计算器,用于一个名为Pangya的游戏,因为我不知道还能做什么 代码: 问题是,当我试图将JTextField ang转换为Int时,我不能输入负数,例如,我试图将孔高度设置为-12,但我得到了一个错误。我不知道如何继续或修复它。所以我谦恭地请求你的帮助 提前感谢。使用带有数字格式化程序的JFormattedTextField代替普通的JFormattedTextField可以将允许的字符减少为数字、十进制分隔符和负号,这样就没有任何理由解析整数、长或双精度 例如:Java 文本字段中出现负数错误,java,swing,jtextfield,parseint,Java,Swing,Jtextfield,Parseint,这是我第一次在这里问你,如果你需要比我提供的更多的信息,尽管问我 我已经学习Java几个星期了,最后我决定尝试制作自己的程序,这是第一个,所以代码会很混乱。它基本上是一个计算器,用于一个名为Pangya的游戏,因为我不知道还能做什么 代码: 问题是,当我试图将JTextField ang转换为Int时,我不能输入负数,例如,我试图将孔高度设置为-12,但我得到了一个错误。我不知道如何继续或修复它。所以我谦恭地请求你的帮助 提前感谢。使用带有数字格式化程序的JFormattedTextField代
DecimalFormat format = new DecimalFormat("#");
JFormattedTextField formattedField = new JFormattedTextField(format);
formattedField.setColumns(15);
使用JFormattedTextField和数字格式化程序可以将允许的字符减少为数字、小数分隔符和负号,这样就没有任何理由解析整型、长型或双精度
例如:
DecimalFormat format = new DecimalFormat("#");
JFormattedTextField formattedField = new JFormattedTextField(format);
formattedField.setColumns(15);
您正在侦听插入,但需要侦听更改。在changedUpdate方法中使用parseInt,如下所示:
ang.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
an = Integer.parseInt(ang.getText());
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
}
});
它对我有用。您还需要更改所有其他类似的侦听器方法。您正在侦听插入,但您需要侦听更改。在changedUpdate方法中使用parseInt,如下所示:
ang.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
an = Integer.parseInt(ang.getText());
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
}
});
Integer.parseInt(dis.getText())
它对我有用。您还需要更改所有其他侦听器方法
Integer.parseInt(dis.getText())
抛出NumberFormatException。您可以捕获它,然后告诉用户他输入了错误的值
一个好方法是通过JOptionPane显示对话框
JOptionPane.showMessageDialog(...)
我通常检查ActionListener中的值。然后,当出现错误时,我显示消息并从actionPerformed方法返回。然后用户有另一个机会输入正确的值
您不需要为每个文本字段都设置DocumentListener。您可以稍后获取文本:
class GUI extends JFrame {
private JLabel distance;
private JLabel altura;
private JLabel slope;
private JLabel wind;
private JLabel angle;
private JLabel cheat;
private JLabel toma;
private JLabel spike;
private JLabel result;
public JTextField dis;
public JTextField alt;
public JTextField sl;
public JTextField w;
public JTextField ang;
private JButton res;
int d;
int al;
int s;
int win;
int an;
double H;
public GUI() {
super("Pangya Calculator");
setLayout(null);
distance = new JLabel("Enter hole distance:");
altura = new JLabel("Enter hole height:");
slope = new JLabel("Enter slope resistance:");
wind = new JLabel("Enter wind power:");
angle = new JLabel("Enter wind angle:");
cheat = new JLabel("Are you using cheat?");
toma = new JLabel("Tomahawk");
spike = new JLabel("Spike");
result = new JLabel("You must hit with backspin:");
dis = new JTextField();
alt = new JTextField();
sl = new JTextField();
w = new JTextField();
ang = new JTextField();
res = new JButton("Calculate");
Dimension size1 = distance.getPreferredSize();
Dimension size2 = altura.getPreferredSize();
Dimension size3 = slope.getPreferredSize();
Dimension size4 = wind.getPreferredSize();
Dimension size5 = angle.getPreferredSize();
Dimension size6 = cheat.getPreferredSize();
Dimension size7 = toma.getPreferredSize();
Dimension size8 = spike.getPreferredSize();
Dimension size9 = result.getPreferredSize();
Dimension size10 = res.getPreferredSize();
distance.setBounds(12, 50, size1.width, size1.height);
altura.setBounds(12, 150, size2.width, size2.height);
slope.setBounds(12, 250, size3.width, size3.height);
wind.setBounds(12, 350, size4.width, size4.height);
angle.setBounds(12, 450, size5.width, size5.height);
cheat.setBounds(250, 50, size6.width, size6.height);
toma.setBounds(250, 150, size7.width, size7.height);
spike.setBounds(350, 150, size8.width, size8.height);
result.setBounds(250, 350, size9.width, size8.height);
dis.setBounds(12, 75, size1.width, size1.height);
alt.setBounds(12, 175, size1.width, size1.height);
sl.setBounds(12, 275, size1.width, size1.height);
w.setBounds(12, 375, size1.width, size1.height);
w.setEditable(true);
ang.setBounds(12, 475, size1.width, size1.height);
ang.setEditable(true);
res.setBounds(200, 500, size10.width, size10.height);
res.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
d = Integer.parseInt(dis.getText());
...etc for all fields
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(GUI.this, "One of the values is not a number.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
H = d + al + Math.ceil(Math.sin(an) * win);
JOptionPane.showMessageDialog(null, "You must hit " + H
+ "y with backspin.", "Hit", JOptionPane.PLAIN_MESSAGE);
}
});
add(distance);
add(altura);
add(slope);
add(wind);
add(angle);
add(cheat);
add(toma);
add(spike);
add(result);
add(dis);
抛出NumberFormatException。您可以捕获它,然后告诉用户他输入了错误的值
一个好方法是通过JOptionPane显示对话框
JOptionPane.showMessageDialog(...)
我通常检查ActionListener中的值。然后,当出现错误时,我显示消息并从actionPerformed方法返回。然后用户有另一个机会输入正确的值
您不需要为每个文本字段都设置DocumentListener。您可以稍后获取文本:
class GUI extends JFrame {
private JLabel distance;
private JLabel altura;
private JLabel slope;
private JLabel wind;
private JLabel angle;
private JLabel cheat;
private JLabel toma;
private JLabel spike;
private JLabel result;
public JTextField dis;
public JTextField alt;
public JTextField sl;
public JTextField w;
public JTextField ang;
private JButton res;
int d;
int al;
int s;
int win;
int an;
double H;
public GUI() {
super("Pangya Calculator");
setLayout(null);
distance = new JLabel("Enter hole distance:");
altura = new JLabel("Enter hole height:");
slope = new JLabel("Enter slope resistance:");
wind = new JLabel("Enter wind power:");
angle = new JLabel("Enter wind angle:");
cheat = new JLabel("Are you using cheat?");
toma = new JLabel("Tomahawk");
spike = new JLabel("Spike");
result = new JLabel("You must hit with backspin:");
dis = new JTextField();
alt = new JTextField();
sl = new JTextField();
w = new JTextField();
ang = new JTextField();
res = new JButton("Calculate");
Dimension size1 = distance.getPreferredSize();
Dimension size2 = altura.getPreferredSize();
Dimension size3 = slope.getPreferredSize();
Dimension size4 = wind.getPreferredSize();
Dimension size5 = angle.getPreferredSize();
Dimension size6 = cheat.getPreferredSize();
Dimension size7 = toma.getPreferredSize();
Dimension size8 = spike.getPreferredSize();
Dimension size9 = result.getPreferredSize();
Dimension size10 = res.getPreferredSize();
distance.setBounds(12, 50, size1.width, size1.height);
altura.setBounds(12, 150, size2.width, size2.height);
slope.setBounds(12, 250, size3.width, size3.height);
wind.setBounds(12, 350, size4.width, size4.height);
angle.setBounds(12, 450, size5.width, size5.height);
cheat.setBounds(250, 50, size6.width, size6.height);
toma.setBounds(250, 150, size7.width, size7.height);
spike.setBounds(350, 150, size8.width, size8.height);
result.setBounds(250, 350, size9.width, size8.height);
dis.setBounds(12, 75, size1.width, size1.height);
alt.setBounds(12, 175, size1.width, size1.height);
sl.setBounds(12, 275, size1.width, size1.height);
w.setBounds(12, 375, size1.width, size1.height);
w.setEditable(true);
ang.setBounds(12, 475, size1.width, size1.height);
ang.setEditable(true);
res.setBounds(200, 500, size10.width, size10.height);
res.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
d = Integer.parseInt(dis.getText());
...etc for all fields
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(GUI.this, "One of the values is not a number.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
H = d + al + Math.ceil(Math.sin(an) * win);
JOptionPane.showMessageDialog(null, "You must hit " + H
+ "y with backspin.", "Hit", JOptionPane.PLAIN_MESSAGE);
}
});
add(distance);
add(altura);
add(slope);
add(wind);
add(angle);
add(cheat);
add(toma);
add(spike);
add(result);
add(dis);
试试这个:
public void insertUpdate(DocumentEvent e) {
try {
an = Integer.parseInt(ang.getText());
}
catch(NumberFormatException ex) {
// doNothing
}
}
试试这个:
public void insertUpdate(DocumentEvent e) {
try {
an = Integer.parseInt(ang.getText());
}
catch(NumberFormatException ex) {
// doNothing
}
}
首先,始终将整数解析嵌入try{}catch语句中,例如
try {
int myInt = Integer.parseInt(someString);
} catch (NumberFormatException e) {
// not a number/cannot parse it
}
检查下面的答案,因为这也是一个问题。首先,请始终在try{}catch语句中嵌入整数解析,例如
try {
int myInt = Integer.parseInt(someString);
} catch (NumberFormatException e) {
// not a number/cannot parse it
}
检查下面的答案,因为这也是一个问题刚试过,结果还是一样,我想问题出在a=Integer.parseIntdis.getText,但我不知道如何解决。编辑:哦,很抱歉,我在手机上读到了,它把你的信息剪了一半,我将删除解析部分以查看发生了什么。@user1261239:例如,请参见编辑。希望你不介意编辑。请随意更正。顺便说一句,我试着这么做,但现在我仅仅点击文本字段就出现了一个错误。我刚刚发现,如果我输入一个正数,按计算,然后在所说的数字之前加上一个-它就可以工作了。但是我不能直接添加它。@user1261239在我的答案中是关于JFormattedTextField的教程的链接,其中包含了很好的示例,请您阅读了这个教程吗?刚刚尝试过,它还是一样的,我想问题出在a=Integer.parseIntdis.getText中,但我不知道如何解决它。编辑:哦,很抱歉,我正在从手机上阅读,它将您的消息截短了一半,我将删除解析部分,看看发生了什么。@user1261239:例如,请参阅编辑。希望你不介意编辑。请随意更正。顺便说一句,我试着这么做,但现在我仅仅点击文本字段就出现了一个错误。我刚刚发现,如果我输入一个正数,按计算,然后在所说的数字之前加上一个-它就可以工作了。但是我不能直接添加它。@user1261239在我的答案中是关于JFormattedTextField的教程的链接,并且包含了很好的示例,请阅读本教程。为了更快地获得帮助,请在问题上发布一条,而不是在pastebin上发布两段代码。为了更快地获得帮助,请在问题上发布一条,与pastebin的两段代码相反。如果我这样做,它会让我键入负数,但不会在公式中加减。好吧,我的错。它不捕获changedUpdate,但捕获insertUpdate。问题是当用户输入一个字符时;在“提交”按钮之前,将调用这些方法。所以先处理“-”号,然后处理“-1”。第一个显然抛出了一个异常。我不认为你在听
呃是最好的办法。但是你可以抓住这个例外,如果你想继续前进。是的,我刚刚意识到,如果我让异常消失,它会很好地工作。最重要的是,如果我先把数字放在“-”之前,它会出现在“-”之前,但这是一个小错误。如果我这样做,它会让我键入负数,但它不会在公式中加减。好吧,我的错。它不捕获changedUpdate,但捕获insertUpdate。问题是当用户输入一个字符时;在“提交”按钮之前,将调用这些方法。所以先处理“-”号,然后处理“-1”。第一个显然抛出了一个异常。我不认为documentlistener是最好的方法。但是你可以抓住这个例外,如果你想继续前进。是的,我刚刚意识到,如果我让异常消失,它会很好地工作。最重要的是,如果我把“-”放在第一位,数字会出现在“-”之前,但这只是一个小错误。问题是我需要使用负值。Integer.parseInt应该能够解析负值。那些数字真的是整数吗?如果它们包含一个小数点,它们将不会作为整数进行解析。问题是我需要使用负值。Integer.parseInt应该能够解析负值。那些数字真的是整数吗?如果它们包含一个小数点,它们将不会作为整数进行解析。如果catch不做任何操作,它不会从最终公式中添加或减去。如果catch不做任何操作,它不会从最终公式中添加或减去。这是一个注释,而不是一个答案;这是评论,不是回答;