1

这是我第一次在这里问,所以如果您需要比我提供的更多信息,请问我。

我已经学习 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 并且我得到一个错误。我不知道如何继续或修复它。所以我谦虚地请求你的帮助。

提前致谢。

4

5 回答 5

3

使用JFormattedTextField而不是普通的JTextFieldJFormattedTextField使用数字格式化程序可以将允许的字符减少为数字小数分隔符负号,然后没有任何解析的原因IntegerLong或者Double

例如:

  DecimalFormat format = new DecimalFormat("#");
  JFormattedTextField formattedField = new JFormattedTextField(format);
  formattedField.setColumns(15);
于 2012-03-10T16:48:59.063 回答
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);
于 2012-03-10T17:00:02.323 回答
0

首先,始终将您的整数解析嵌入到try { } catch语句中,例如..

try {
     int myInt = Integer.parseInt(someString);
} catch (NumberFormatException e) {
     // not a number/cannot parse it
}

检查下面的答案,因为那也是一个问题

于 2012-03-10T18:07:52.130 回答
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) {
            }
        });

这个对我有用。您还需要像这样更改所有其他侦听器方法。

于 2012-03-10T16:59:34.050 回答
0

尝试这个:

public void insertUpdate(DocumentEvent e) {
     try {
            an = Integer.parseInt(ang.getText());
     }
     catch(NumberFormatException ex) {
            // doNothing
     }
}
于 2012-03-10T17:22:18.407 回答