这是我第一次在这里问,所以如果您需要比我提供的更多信息,请问我。
我已经学习 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 并且我得到一个错误。我不知道如何继续或修复它。所以我谦虚地请求你的帮助。
提前致谢。