创建一个新的JCheckBox使我的界面无限次打开

时间:2018-01-23 04:33:58

标签: java swing jframe jcheckbox

我之前有一些代码工作得非常好,但现在我正在尝试添加另一个JCheckBox,当我运行代码时,界面会在无限循环中弹出。导致它的代码是我创建新的JCheckBox" marathonCheck。"如果没有该行(并且没有其他marathonCheck操作),接口将正常打开。我不知道发生了什么事。

public class GUI extends JFrame implements ActionListener{


    JCheckBox rM;
    JButton rS;
    JFrame jFrame;
    private boolean runeMystery;
    private boolean runScript = false;


    public GUI() {

        jFrame = new JFrame("All in One Start-Up Runecrafter");
        jFrame.setSize(400, 400);
        jFrame.setVisible(true);
        jFrame.setLayout(new GridLayout(0, 1));

        JCheckBox marathonCheck = new JCheckBox("Would you like to enable marathon mode?"); //<--- THIS IS A PROBLEM
        marathonCheck.setSelected(true);
        if(marathonCheck.isSelected()){
        rM = new JCheckBox("Rune Mysteries Completed?");
        }

        rS = new JButton("Run Script");

        jFrame.add(marathonCheck);

        if(marathonCheck.isSelected()){
        jFrame.add(rM);
        }

        jFrame.add(rS);

        rS.addActionListener(this);

    }


    public void actionPerformed(ActionEvent ae){

        runeMystery = rM.isSelected();
        runScript = true;
        jFrame.setVisible(false);
        dispose();

    }

1 个答案:

答案 0 :(得分:0)

将文本放入JCheckBox的参数中也许并不是那么糟糕,但它可以帮助解决您的问题,也需要一个main来运行它并让它运行

   public GUI() {
    jFrame = new JFrame("All in One Start-Up Runecrafter");
    jFrame.setSize(400, 400);
    jFrame.setVisible(true);
    jFrame.setLayout(new GridLayout(0, 1));
    JCheckBox marathonCheck = new JCheckBox(); 
    marathonCheck.setText("Would you like to enable marathon mode?");
    marathonCheck.setSelected(true);
    if(marathonCheck.isSelected()){
    rM = new JCheckBox();
    rM.setText("Rune Mysteries Completed?");
    }

    rS = new JButton("Run Script");

    jFrame.add(marathonCheck);

    if(marathonCheck.isSelected()){
    jFrame.add(rM);
    }

    jFrame.add(rS);

    rS.addActionListener(this);

}