ActionListener 自身循环

发布于 2025-01-11 16:01:05 字数 1789 浏览 0 评论 0原文

好吧,实际上我什至觉得问这个问题很愚蠢,但是:ActionListener 是否有可能自行循环?情况如下(有点简略,因为它本身有很多健全的代码,对问题没有任何作用)

JMenuItem Menu=new JMenuItem();
Menu.addActionListener(e -> {
            JDialog Dialog=new JDialog();
            JTextField Name= new JTextField("",30);
            JFormattedTextField ID =new JFormattedTextField(NumberFormat.getIntegerInstance());
            ID.setColumns(8);
            JButton Enter=new JButton();
            \\GUI building stuff
            Enter.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    Integer ImportID=((Number) ID.getValue()).intValue();
                    String ImportName= Name.getText();
                    System.out.println("Eingaben\n"+ImportID);
                    System.out.println(ImportName);
                    //loads of different stuff
                    Dialog.dispose();
                    System.out.println(ImportID);
                    System.out.println(ImportName);
                    //ID.setValue(null);
                    //Name.setText(null);
                    //ImportID=null;
                    //ImportID=null;
                    System.out.println("Fertig!");
                }
            });
           
});

现在这个菜单将导致一个旨在用作导入对话框的对话框。现在发生的情况是,第一次使用任何用户输入数据时,该数据都会正确传播。但是,如果用户尝试第二次进行输入,则第一个值将被传递 - 即使处理了对话框,第一次传递的数据也不应该被访问,对吧?
我尝试用 e.getSource 替换内部 ActionListener,但这不起作用。将变量或字段设置为 null 也没有帮助。
我知道将 ActionListener 放置在 ActionListener 中可能是不好的形式。

编辑:我应该解释一下这是如何工作的:选择 MenuItem(位于正确 GUI 的菜单栏中)并调用一个对话框,用户在其中输入两条数据并选择一些文件。选定的文件将被复制到新目录中,并且输入的数据将被处理为两个文本文件。之后,对话框被设置为不可见,并且发生一些与数据无关的不同处理。该过程完成后,应该将所有事情都结束,以便可以再次调用该对话框。但是,如果我关闭应用程序,则不会发生此错误。

Well, I actually feel stupid for even asking this question but: Is it possible for an ActionListener to loop back on it self? The situation is as follows (kind shorthandish because it is a lot of in itself sound code that does nothing to the problem)

JMenuItem Menu=new JMenuItem();
Menu.addActionListener(e -> {
            JDialog Dialog=new JDialog();
            JTextField Name= new JTextField("",30);
            JFormattedTextField ID =new JFormattedTextField(NumberFormat.getIntegerInstance());
            ID.setColumns(8);
            JButton Enter=new JButton();
            \\GUI building stuff
            Enter.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    Integer ImportID=((Number) ID.getValue()).intValue();
                    String ImportName= Name.getText();
                    System.out.println("Eingaben\n"+ImportID);
                    System.out.println(ImportName);
                    //loads of different stuff
                    Dialog.dispose();
                    System.out.println(ImportID);
                    System.out.println(ImportName);
                    //ID.setValue(null);
                    //Name.setText(null);
                    //ImportID=null;
                    //ImportID=null;
                    System.out.println("Fertig!");
                }
            });
           
});

Now this menu leads to a dialog intended as an import dialog. What happens now is that the first time any user input data is used this data is propagated correctly. But if the user attempts make a input a second time the first and the second values are handed through - even with the disposing of the Dialog the data of the first go through shouldn't even be accessible, right?
I tried replacing the inner ActionListener with an e.getSource, which didn't work. Setting the variables or the Field to null doesn't help either.
I am aware that placing an ActionListener within an ActionListener is propably bad form.

Edit: I should explain how this works: The MenuItem (which sits inside the menubar of a proper GUI) is selected and calls forth a Dialog where the user enters two pieces of data and selects some files. Selected files are copied into a new directory and the entered data is processed into two text files. After that happened the Dialog is set to invisible and some different and to the data unrelated processing occurs. This processign being finished things are supposed to be wrapped up so that the dialog can be called upon again. If however I close the application this error does not occur.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文