这是我第一次问这里,如果您需要的信息比我提供的更多,请问我。
我已经学习了几个星期的Java,最后我决定尝试创建自己的程序,这是第一个,所以代码将变得一团糟。它基本上是一个名为Pangya的游戏的计算器,因为我不知道还能做什么。
代码:
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Pangya {
public static void main(String[] args) {
GUI g = new GUI();
g.setSize(500, 600);
g.setVisible(true);
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
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);
dis.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
d = Integer.parseInt(dis.getText());
}
});
alt.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
al = Integer.parseInt(alt.getText());
}
});
sl.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
s = Integer.parseInt(sl.getText());
}
});
ang.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
an = Integer.parseInt(ang.getText());
}
});
w.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
win = Integer.parseInt(w.getText());
}
});
res.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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);
add(alt);
add(sl);
add(w);
add(ang);
add(res);
}
}
问题在于,当我试图将JTextField“ang”转换为Int时,我不能放置负数,例如我尝试将孔高度设置为-12并且我得到一个错误。我不知道如何继续或修复它。所以我谦卑地请求你的帮助。
提前致谢。
答案 0 :(得分:3)
使用JFormattedTextField代替普通JTextField,JFormattedTextField
Number formatter
可以将chars
缩减为Numbers
,decimal separator
和negative sing
,然后解析Integer
,Long
或Double
例如:
DecimalFormat format = new DecimalFormat("#");
JFormattedTextField formattedField = new JFormattedTextField(format);
formattedField.setColumns(15);
答案 1 :(得分:1)
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);
答案 2 :(得分:0)
您正在侦听插入内容,但您需要侦听更改。在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) {
}
});
它对我有用。此外,您还需要更改所有其他侦听器方法。
答案 3 :(得分:0)
试试这个:
public void insertUpdate(DocumentEvent e) {
try {
an = Integer.parseInt(ang.getText());
}
catch(NumberFormatException ex) {
// doNothing
}
}
答案 4 :(得分:0)
首先,总是将你的整数解析嵌入到try { } catch
语句中,例如..
try {
int myInt = Integer.parseInt(someString);
} catch (NumberFormatException e) {
// not a number/cannot parse it
}
检查下面的答案,因为这也是一个问题