【问题标题】:Basic GUI java program in Eclipse keeps crashingEclipse中的基本GUI java程序不断崩溃
【发布时间】:2015-04-08 17:48:35
【问题描述】:

我正在尝试制作一个游戏,在上述游戏中,有 21 根棍子,每个人轮流拿 1-4 根棍子,直到没有棍子,如果你不能再拿棍子,你就输了。我已经在 Eclipse 中成功制作了这个程序,但现在我想向它添加 GUI,所以我必须更改代码。此代码不完整,但每当我按下作为我的 actionListener 的 Go 按钮时它就会崩溃。我会在文本字段中输入一个数字,然后按 go,它就会崩溃。我该如何解决这个问题?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Sticks extends JFrame {

JButton Go;
JTextField tf1, tf2;
static JTextField sttf;
JLabel startTake;
static JLabel errorTake;
JLabel uTake;
JLabel compTake;

public Sticks() {
    setLayout(new GridLayout(5, 2, 5, 5));

    startTake = new JLabel("How many sticks do you want to take? (1-4)");
    add(startTake);

    sttf = new JTextField();
    add(sttf);

    errorTake = new JLabel("Hello");
    add(errorTake);

    Go = new JButton("Go");
    add(Go);

    uTake = new JLabel("");
    add(uTake);

    compTake = new JLabel("");
    add(compTake);

    // tf1 = new JTextField();
    // add(tf1);

    // TakeP = new JLabel("One stick taken");
    // add(TakeP);

    event e = new event();
    Go.addActionListener(e);
}

public static class event implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        int numSticks = 21;
        int numToTake = 0;
        int randomNum = 0;

        while (numSticks > 0) {
            try {
                int num = (int) (Double.parseDouble(sttf.getText()));
                int NumSticks = numSticks - num;

                    errorTake.setText("There are: " + numSticks + " left");

                    Robot Rob = new Robot();
                    numToTake = (int)Math.random() * 4 + 1;
                    errorTake.setText("There are: " + numSticks + " left");
                }
             catch (Exception ex) {
                ex.printStackTrace();;errorTake.setText("There is a problem");
            }
        }
    }
}

public static void main(String[] args) {
    Sticks gui = new Sticks();
    gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setSize(600, 200);

    gui.setTitle("Nice Game");
}
}

【问题讨论】:

  • catch (Exception exx) { errorTake.setText("Numbers only!");改成catch (Exception exx) { exx.printStackTrace(); errorTake.setText("Numbers only!");然后复制/粘贴异常输出。
  • 我没有得到堆栈跟踪

标签: java eclipse swing user-interface crash


【解决方案1】:

每次您单击“开始”按钮时,您的 actionPerformed 都会触发,并且它根本不等待用户输入。这是你的问题线。

public static class event implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        //...
                int num = (int) (Double.parseDouble(sttf.getText()));
        //...
    }
}

sttf.getText() 总是返回 "" 因为 sttf 是空的,程序不像 Scanner(System.in) 那样等待用户输入。

没有任何异常是有道理的,因为它只是运行并完成游戏,而没有给用户足够的时间输入任何内容。但是,您确定控制台不会打印“仅限数字!”吗?因为我之前从未尝试过解析空字符串。


好吧,我读错了,对不起。

您的 actionListener 每次单击时都会生成一个新游戏,因为您在每次单击时将 numSticks 设置为 21。展望未来,我认为这不是一个好主意,除非您想在比赛结束之前一直使用相同数量的棍子。同样的事情。如果你在 sttf 中输入一个值,程序不会等待你改变它,因为它会一直使用那个值直到你的 while 循环结束。

【讨论】:

  • 你看我没有得到任何东西。当我启动程序时,我创建的 java 窗口出现了,我在文本字段中输入一个值,然后按 go,它就停止工作了。
  • 哦,知道了。编辑我的答案。
  • “停止工作”是指冻结吗?因为这很可能意味着您处于无限循环中。
  • 我认为你是对的,它可能是一个无限循环,我已经更新了我上面的代码,但仍然遇到同样的问题。没有打印 StackTraces,现在怎么办?
猜你喜欢
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
相关资源
最近更新 更多