java - 如何退出 ActionListener 中的方法

标签 java swing methods actionlistener jtextfield

我有一个连接到 JTextFieldActionListener,并且想要输入一些内容,以便它退出 ActionListener 所在的方法。

代码:

main() {
    Security(x,x,x);
}
public void Security(JTextArea out, JTextField in) {
        in.setText("");
        in.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (in.getText().contains("exitsys")) {
                    out.append("Security:Security System Deactivated\n");
                    return;
                }
                in.setText("");
            }
        });
        out.append("Security:Security System Activated\n");
        fileWrite(":SYSTEM_INITIATED@" + time(), out);
    }

我想输入“exitsys”并返回到主类方法“main()”

fileWrite 方法使用 PrintWriter 输出数据。

问题摘要:我尝试调用 return;但它没有返回到方法 main(),我该如何解决这个问题?

最佳答案

基本上,您需要的是某种模式对话框,它允许您在对话框可见时有效地停止程序的执行,直到对话框被解除(关闭),此时执行将继续...

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JDialog dialog = new JDialog();
                dialog.setTitle("Testing");
                dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                dialog.add(new TestPane());
                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);

                System.out.println("Now back in the main...");
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField field;

        public TestPane() {

            setLayout(new GridBagLayout());

            field = new JTextField(10);
            field.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if ("exitsys".equals(field.getText())) {
                        SwingUtilities.getWindowAncestor(field).dispose();
                    }
                }
            });

            add(field);

        }

    }

}

参见How to Make Dialogs了解更多详情

关于java - 如何退出 ActionListener 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27560703/

相关文章:

java - Elasticsearch - EdgeNgram + highlight + term_vector = 不好的亮点

Java线程sleep()方法

java - 更新 Swing 组件,同时阻止 GUI

java - 在java中添加更多标签

java - 比较方法参数类型,过滤具有特定签名的类的方法

java - org.apache.lucene.analysis.StandardAnalyzer 无法解析

java - Android如何用本地类覆盖远程包中的类

java - 如何在java swing中创建剧透效果?

python - @staticmethod 和@classmethod 的区别

java - 从一种方法在另一种方法中打印数组