Jdialog 在可用之前就消失了
我有一个奇怪的问题:从菜单栏我(或用户)可以调用两个操作。一个导入一些数据,另一个删除一些数据。两者都是通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将 ActionListenerer 的 actionPerformed 的代码移至新方法
void showEntfernenFenster()
并执行例如:
actionPerformed
发生在处理 GUI 事件的单线程上AWT 队列。那么 GUI 就无能为力了。AWTEventQueue.invokeLater
或SwingUtilities.invokeLater
(相同)确实会在处理actionPerformed
后调用传递的“Runnable”。然后,GUI 是响应式的,重绘等等,一般来说,不要在 actionPerformed 中做很长的事情。原因:
为了重绘、使多个屏幕区域无效,并且所有这些都以非并行方式(顺序),跨编程语言甚至操作系统级别的许多库都使用这种简化技术:事件队列和处理它们的一个线程。
You could try to move the code of the ActionListenerer's actionPerformed to a new method
void showEntfernenFenster()
and doFor instance in:
actionPerformed
happens on the single thread handling GUI events in the AWT queue. Then little else for the GUI can be done.AWTEventQueue.invokeLater
orSwingUtilities.invokeLater
(identical) will indeed call the passed "Runnable" after theactionPerformed
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.