Jdialog 在可用之前就消失了

发布于 2025-01-11 00:08:57 字数 4509 浏览 3 评论 0原文

我有一个奇怪的问题:从菜单栏我(或用户)可以调用两个操作。一个导入一些数据,另一个删除一些数据。两者都是通过 JDialog 呈现的。现在奇怪的是,两者实际上共享相同的布局,但虽然导入对话框工作得很好,但删除对话框在任何交互发生之前就消失了。我确实分别给他们打电话。

为了进行比较,请考虑以下部分:

  • 导入对话框
 JDialog ImportFenster= new JDialog(Fenster,
                    "Import von Nutzern");

            JTextField Name= new JTextField("Vorname [Leer] Name",30);
            JFormattedTextField ID =new JFormattedTextField(
                    NumberFormat.getIntegerInstance());
            ID.setColumns(8);

            JButton Enter = new JButton("OK");
            JButton Abort =new JButton("Abbruch");
            JLabel InfoID= new JLabel("Wird keine ID angegeben, wird eine automatisch erzeugt");

            GridBagLayout ImportLayout= new GridBagLayout();
            GridBagConstraints LayoutConstraints=new GridBagConstraints();

            ImportFenster.setLayout(ImportLayout);

            LayoutConstraints.fill=GridBagConstraints.VERTICAL;
            LayoutConstraints.gridwidth=3;
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=0;
            LayoutConstraints.ipadx=5;
            LayoutConstraints.ipady=5;
            ImportFenster.add(InfoID,LayoutConstraints);
            LayoutConstraints.gridwidth=1;
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=1;
            LayoutConstraints.gridy=1;
            ImportFenster.add(ID,LayoutConstraints);
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=1;
            LayoutConstraints.gridy=2;
            ImportFenster.add(Name,LayoutConstraints);
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=3;
            LayoutConstraints.gridy=3;
            ImportFenster.add(Enter,LayoutConstraints);
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=3;
            ImportFenster.add(Abort,LayoutConstraints);
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=1;
            ImportFenster.add(new JLabel("Nutzer-ID:"),LayoutConstraints);
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=2;
            ImportFenster.add(new JLabel("Nutzer-Name:"),LayoutConstraints);


            ImportFenster.setSize(750,350);
            ImportFenster.setVisible(true);
  • 删除对话框
JDialog EntfernenFenster= new JDialog(Fenster,
                        "Löschen von Nutzern");
                JFormattedTextField ID =new JFormattedTextField(
                        NumberFormat.getIntegerInstance());
                ID.setColumns(8);
                System.out.println("Dialog:"+EntfernenFenster.isShowing());
                JButton Enter = new JButton("OK");
                JButton Abort =new JButton("Abbruch");
                System.out.println("Knöppe sind da:"+Enter.isShowing()+"\t"+Abort.isShowing());
                GridBagLayout EntfernenLayout= new GridBagLayout();
                EntfernenFenster.setLayout(EntfernenLayout);
                GridBagConstraints LayoutConstraints=new GridBagConstraints();
                LayoutConstraints.fill=GridBagConstraints.VERTICAL;
                LayoutConstraints.gridwidth=1;
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=1;
                LayoutConstraints.gridy=1;
                EntfernenFenster.add(ID,LayoutConstraints);
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=0;
                LayoutConstraints.gridy=1;
                EntfernenFenster.add(new JLabel("Nutzer-ID:"));
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=2;
                LayoutConstraints.gridy=2;
                EntfernenFenster.add(Enter,LayoutConstraints);
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=0;
                LayoutConstraints.gridy=2;
                EntfernenFenster.add(Abort,LayoutConstraints);

                EntfernenFenster.setSize(500, 200);
                EntfernenFenster.setVisible(true);

我想知道我的错误在哪里。当然还有如何解决这个问题。

I have a strange problem: From a MenuBar I (or the user) can call two actions. One does importing some data, the other deletes some data. Both are presented through a JDialog. Now the weird thing is that both share effectively the same layout and yet while the import dialogue works perfectly, the delete dialogue vanished before any interaction can happen. I do call them seperately.

For comparison consider these parts:

  • the Import dialogue
 JDialog ImportFenster= new JDialog(Fenster,
                    "Import von Nutzern");

            JTextField Name= new JTextField("Vorname [Leer] Name",30);
            JFormattedTextField ID =new JFormattedTextField(
                    NumberFormat.getIntegerInstance());
            ID.setColumns(8);

            JButton Enter = new JButton("OK");
            JButton Abort =new JButton("Abbruch");
            JLabel InfoID= new JLabel("Wird keine ID angegeben, wird eine automatisch erzeugt");

            GridBagLayout ImportLayout= new GridBagLayout();
            GridBagConstraints LayoutConstraints=new GridBagConstraints();

            ImportFenster.setLayout(ImportLayout);

            LayoutConstraints.fill=GridBagConstraints.VERTICAL;
            LayoutConstraints.gridwidth=3;
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=0;
            LayoutConstraints.ipadx=5;
            LayoutConstraints.ipady=5;
            ImportFenster.add(InfoID,LayoutConstraints);
            LayoutConstraints.gridwidth=1;
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=1;
            LayoutConstraints.gridy=1;
            ImportFenster.add(ID,LayoutConstraints);
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=1;
            LayoutConstraints.gridy=2;
            ImportFenster.add(Name,LayoutConstraints);
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=3;
            LayoutConstraints.gridy=3;
            ImportFenster.add(Enter,LayoutConstraints);
            LayoutConstraints.ipadx=10;
            LayoutConstraints.ipady=25;
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=3;
            ImportFenster.add(Abort,LayoutConstraints);
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=1;
            ImportFenster.add(new JLabel("Nutzer-ID:"),LayoutConstraints);
            LayoutConstraints.gridx=0;
            LayoutConstraints.gridy=2;
            ImportFenster.add(new JLabel("Nutzer-Name:"),LayoutConstraints);


            ImportFenster.setSize(750,350);
            ImportFenster.setVisible(true);
  • the delete dialogue
JDialog EntfernenFenster= new JDialog(Fenster,
                        "Löschen von Nutzern");
                JFormattedTextField ID =new JFormattedTextField(
                        NumberFormat.getIntegerInstance());
                ID.setColumns(8);
                System.out.println("Dialog:"+EntfernenFenster.isShowing());
                JButton Enter = new JButton("OK");
                JButton Abort =new JButton("Abbruch");
                System.out.println("Knöppe sind da:"+Enter.isShowing()+"\t"+Abort.isShowing());
                GridBagLayout EntfernenLayout= new GridBagLayout();
                EntfernenFenster.setLayout(EntfernenLayout);
                GridBagConstraints LayoutConstraints=new GridBagConstraints();
                LayoutConstraints.fill=GridBagConstraints.VERTICAL;
                LayoutConstraints.gridwidth=1;
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=1;
                LayoutConstraints.gridy=1;
                EntfernenFenster.add(ID,LayoutConstraints);
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=0;
                LayoutConstraints.gridy=1;
                EntfernenFenster.add(new JLabel("Nutzer-ID:"));
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=2;
                LayoutConstraints.gridy=2;
                EntfernenFenster.add(Enter,LayoutConstraints);
                LayoutConstraints.ipadx=10;
                LayoutConstraints.ipady=25;
                LayoutConstraints.gridx=0;
                LayoutConstraints.gridy=2;
                EntfernenFenster.add(Abort,LayoutConstraints);

                EntfernenFenster.setSize(500, 200);
                EntfernenFenster.setVisible(true);

I would like to know where my mistake is. And of course how to fix this.

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

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

发布评论

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

评论(1

茶花眉 2025-01-18 00:08:57

您可以尝试将 ActionListenerer 的 actionPerformed 的代码移至新方法 void showEntfernenFenster() 并执行

SwingUtilities.invokeLater(() -> showEntfernenFenster());

例如:

JMenuItem menuItem = ...
menuItem.addActionListener(evt ->
    SwingUtilities.invokeLater(() -> showEntfernenFenster()));

actionPerformed 发生在处理 GUI 事件的单线程上AWT 队列。那么 GUI 就无能为力了。 AWTEventQueue.invokeLaterSwingUtilities.invokeLater (相同)确实会在处理 actionPerformed 后调用传递的“Runnable”。然后,GUI 是响应式的,重绘等等,一般来说,不要在 actionPerformed 中做很长的事情。

原因:

为了重绘、使多个屏幕区域无效,并且所有这些都以非并行方式(顺序),跨编程语言甚至操作系统级别的许多库都使用这种简化技术:事件队列和处理它们的一个线程。

You could try to move the code of the ActionListenerer's actionPerformed to a new method void showEntfernenFenster() and do

SwingUtilities.invokeLater(() -> showEntfernenFenster());

For instance in:

JMenuItem menuItem = ...
menuItem.addActionListener(evt ->
    SwingUtilities.invokeLater(() -> showEntfernenFenster()));

actionPerformed happens on the single thread handling GUI events in the AWT queue. Then little else for the GUI can be done. AWTEventQueue.invokeLater or SwingUtilities.invokeLater (identical) will indeed call the passed "Runnable" after the actionPerformed is processed. And then again the GUI is responsive, redrawing etcetera, In general do not do long things in actionPerformed.

The why:

In order to redraw, invalidate multiple screen regions, and all in a non-parallel manner - sequentially -, many libraries across programming languages and even on operating system level use this simplifying technique: a queue of events and one thread handling them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文