连续循环验证

发布于 2025-01-11 00:03:22 字数 654 浏览 0 评论 0原文

我目前正在尝试找出一种方法,如何将验证循环到按下按钮时的位置,它将验证 JTextfield 输入,然后在按下“确定”后在对话框中打印一些内容,它将继续循环此验证,直到达到 times 的值。我假设我会使用 for 循环,但我真的很困惑它的输入位置。当它达到时间值后,我只需 frame.setVisible(false) 并隐藏它。

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         String txtField = testTextField.getText();
          int times = 3;
          if (firstNameInvalid(txtField) == false) {
             JOptionPane.showMessageDialog(null, "input valid text field");
          } else {
              JOptionPane.showMessageDialog(null, "gratz, onto the next !!");
          }
     }
});

I am currently trying to figure out a way how to loop a validation to where when a button is pressed, it will validate the JTextfield inputs then print something in a dialog in which after "OK" is pressed, it will keep cycling this validation till it hits the value of times. I assume I would use a for loop but I am genuinely confused on where it would be inputted. After it has reached the value of the times, I would just frame.setVisible(false) and hide it.

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         String txtField = testTextField.getText();
          int times = 3;
          if (firstNameInvalid(txtField) == false) {
             JOptionPane.showMessageDialog(null, "input valid text field");
          } else {
              JOptionPane.showMessageDialog(null, "gratz, onto the next !!");
          }
     }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无言温柔 2025-01-18 00:03:22

我假设我会使用 for 循环

不,你不应该。虽然此解决方案适用于线性控制台程序,但对于事件驱动的 GUI 代码来说这不是一个可行的解决方案。

相反,创建状态字段、变量,在给定输入时更改它们,并将程序的响应基于这些状态字段所保存的值。

主要目标是让用户输入一个名称,然后将该名称存储到一个数组列表中,然后框架将自行刷新并允许输入和存储另一个名称,这个循环将继续,直到到达“次变量。

然后你可以想象创建一个 List; nameList = new ArrayList<>(); 您的关键变量,此列表的 .size() 是您的程序遵循的状态

每次激活 ActionListener 时,它都会从 JTextField 获取文本并将其添加到 nameList 中。然后它检查 nameList.size() 是否等于您的 times 变量,您就完成了,然后需要在完成任务时执行所需的任何处理。

但同样,for 循环在这里没有帮助,因为您实际上并没有循环。如果您使用 for 循环,就像获取扫描仪输入时一样,您将通过阻止其事件线程来冻结 GUI。事件驱动程序中的所有内容都应该由事件驱动:监听事件并响应事件。

I assume I would use a for loop

No, you shouldn't. While this solution works for linear console programs, this is not a viable solution for event driven GUI code.

Instead, create state fields, variables, that you change as input is given, and base the program's response to values held by these state fields.

the main goal is for the user to enter a name, that name then is stored into an arraylist, then the frame will refresh itself and allow for another name to be inputted and stored, this cycle will continue till it reaches the 'times' variable.

Then you could conceivably make an List<String> nameList = new ArrayList<>(); your key variable, and the .size() of this list the state that your program follows.

Each time the ActionListener is activated, it gets the text from the JTextField and adds it to the nameList. Then it checks the nameList.size() if equal to your times variable, you're done, and then need to do whatever processing is required on completion of the task.

But again, a for-loop is not helpful here since you're not actually looping. If you used a for loop, like you would when getting Scanner input, you'd freeze the GUI by blocking its event thread. Everything in an event-driven program should be driven by, well, events: listen to events and respond to events.

指尖上的星空 2025-01-18 00:03:22

跟踪验证输入的次数,与需要循环的次数进行比较,根据需要做出决策

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new InputPane(3, new InputPane.Observer() {
                    @Override
                    public void didCompleteInput(InputPane source, List<String> names) {
                        frame.dispose();
                    }
                }));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class InputPane extends JPanel {

        public interface Observer {
            public void didCompleteInput(InputPane source, List<String> names);
        }

        private JTextField nameField;
        private JButton nextButton;
        private JProgressBar progressBar;
        private JLabel currentCountLabel;

        private int index = 1;

        private List<String> names;
        private Observer observer;

        public InputPane(int count, Observer observer) {
            this.observer = observer;
            setBorder(new EmptyBorder(32, 32, 32, 32));
            nameField = new JTextField(20);
            nextButton = new JButton("Next");
            progressBar = new JProgressBar(0, count);
            progressBar.setValue(1);
            currentCountLabel = new JLabel("1");

            names = new ArrayList<>(count);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.HORIZONTAL;

            JPanel inputPane = new JPanel();
            inputPane.add(new JLabel("Name: "));
            inputPane.add(nameField);

            add(inputPane, gbc);

            JPanel progressPane = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            progressPane.add(progressBar, gbc);
            gbc.gridx++;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0;
            gbc.insets = new Insets(0, 8, 0, 0);
            progressPane.add(currentCountLabel, gbc);
            gbc.gridx++;
            gbc.insets = new Insets(0, 0, 0, 0);
            progressPane.add(new JLabel("/" + Integer.toString(count)));

            gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.HORIZONTAL;
            add(progressPane, gbc);

            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.LINE_END;
            add(nextButton, gbc);

            nextButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = nameField.getText();
                    nameField.setText(null);
                    if (name.isBlank()) {
                        JOptionPane.showMessageDialog(InputPane.this, "Nope");
                    } else {
                        names.add(name);
                        index++;
                        progressBar.setValue(Math.min(index, count));
                        currentCountLabel.setText(Integer.toString(Math.min(index, count)));
                        if (index > count) {
                            observer.didCompleteInput(InputPane.this, names);
                        }
                    }
                }
            });
        }
    }
}

Keep track of the number of times you've validated the input, compare to the number of times you need to loop, make decisions as required

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new InputPane(3, new InputPane.Observer() {
                    @Override
                    public void didCompleteInput(InputPane source, List<String> names) {
                        frame.dispose();
                    }
                }));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class InputPane extends JPanel {

        public interface Observer {
            public void didCompleteInput(InputPane source, List<String> names);
        }

        private JTextField nameField;
        private JButton nextButton;
        private JProgressBar progressBar;
        private JLabel currentCountLabel;

        private int index = 1;

        private List<String> names;
        private Observer observer;

        public InputPane(int count, Observer observer) {
            this.observer = observer;
            setBorder(new EmptyBorder(32, 32, 32, 32));
            nameField = new JTextField(20);
            nextButton = new JButton("Next");
            progressBar = new JProgressBar(0, count);
            progressBar.setValue(1);
            currentCountLabel = new JLabel("1");

            names = new ArrayList<>(count);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.HORIZONTAL;

            JPanel inputPane = new JPanel();
            inputPane.add(new JLabel("Name: "));
            inputPane.add(nameField);

            add(inputPane, gbc);

            JPanel progressPane = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            progressPane.add(progressBar, gbc);
            gbc.gridx++;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0;
            gbc.insets = new Insets(0, 8, 0, 0);
            progressPane.add(currentCountLabel, gbc);
            gbc.gridx++;
            gbc.insets = new Insets(0, 0, 0, 0);
            progressPane.add(new JLabel("/" + Integer.toString(count)));

            gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.HORIZONTAL;
            add(progressPane, gbc);

            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.LINE_END;
            add(nextButton, gbc);

            nextButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = nameField.getText();
                    nameField.setText(null);
                    if (name.isBlank()) {
                        JOptionPane.showMessageDialog(InputPane.this, "Nope");
                    } else {
                        names.add(name);
                        index++;
                        progressBar.setValue(Math.min(index, count));
                        currentCountLabel.setText(Integer.toString(Math.min(index, count)));
                        if (index > count) {
                            observer.didCompleteInput(InputPane.this, names);
                        }
                    }
                }
            });
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文