不确定将逻辑循环放在哪里 [java]

Not sure where to put logic loop [java]

我目前正在尝试为学校开发一个小程序。如果你点击一个复选框,它应该显示其他元素。我在 python 中了解到您需要一个 while 循环,因为程序需要再次遍历相同的行,在那里您检查该框是否被选中,但如果我放置一个循环,整个程序将不会启动。我不明白为什么。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class test extends JFrame {
    private JCheckBox moredetailscheck;
    private JTextField inputfielduser;

    public static void main(String[] args) {
        test venster = new test();
        venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        venster.setSize(800, 600);
        venster.setVisible(true);
        venster.setTitle("true");
        venster.setResizable(false);
    }
    public test() {
        setLayout(new FlowLayout());
        moredetailscheck = new JCheckBox("checkbox", false);
        add(moredetailscheck);
        inputfielduser = new JTextField(15);
        while(true) {   // you want to let the program keep going over these lines
        if(moredetailscheck.isSelected()) {
            add(inputfielduser);
        } 
    }
}

在Java中,AWT启动一个线程自动处理事件;你只要让 main 完成,程序就会一直保持 运行,直到你调用 System.exit。不过,您确实需要事件处理程序,为此存在任意数量的教程。

(顺便说一句,你的无限循环甚至在显示你的 JFrame 之前就出现了。)

If you click on a checkbox it should show other elements.

因此,您可以将侦听器附加到 JCheckBox,这里是 ItemListener,它会在 JCheckBox 的状态发生变化时做出响应。

I learned in python that you need a while loop because the program needs to go over the same lines again where you check if the box is checked

这称为 "polling",线性控制台程序需要它,您需要以 "linear" 的方式不断从用户那里获取输入。在这些类型的程序中,您作为程序员可以完全控制程序代码流,但这不是您想要的。

but if i put a loop the whole program won't start. I don't understand why.

那是因为您现在正在使用事件驱动的 GUI 库,即 Swing 库,并且通过在事件线程上调用 while (true) 循环,您完全阻止了它,使您的 GUI 变得无用。您的程序正在启动,但无法构建 GUI、绘制自身或侦听事件。

解决方案:

  • 摆脱 while (true) 循环。同样,它对简单的控制台程序很有用,但在这种情况下没有用。
  • 将 ItemListener 添加到您的 JCheckBox。您可以在 check box tutorial
  • 中了解如何操作
  • 不要一直向您的 GUI 添加项目。使用 CardLayout 交换视图。该教程可在此处找到:CardLayout tutorial.
  • 或者甚至更好,在启动时将所有 GUI 项目都放在 GUI 上,但使用 JCheckBox 状态 enable/disable 一个项目。

顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。


例如:

import java.awt.event.ItemEvent;
import javax.swing.*;

public class TestCheckBox extends JPanel {
    private static final long serialVersionUID = 1L;
    private JCheckBox moreDetailsCheck = new JCheckBox("More Details", false);
    private JTextField inputFieldUser = new JTextField(15);

    public TestCheckBox() {
        inputFieldUser.setEnabled(false);
        add(moreDetailsCheck);
        add(inputFieldUser);

        // add a listener to the JCheckBox
        moreDetailsCheck.addItemListener(e -> {
            // if checkbox selected, enable the text field. else disable it
            inputFieldUser.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
        });
    }

    private static void createAndShowGui() {
        TestCheckBox mainPanel = new TestCheckBox();

        JFrame frame = new JFrame("Test CheckBox");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}