例外:向容器添加一个窗口如何解决? [英] Exception : adding a window to a container how to solve it?
问题描述
我有一个名为 Preferences.java
的 JDialog
类。该类创建一个构造函数,如:
class Preferences extends javax.swing.JDialog {
Preferences(java.awt。 Frame parent,modal){
super(parent,modal);
// ......
}
}
在我的程序中,我想要这个首选项对话框打开,因为我点击一个按钮从一个 Jframe 表单。在我注册了按钮上的动作监听器后,我写的代码在:
框架fr = new Frame();
首选项p = new Preferences(fr,false);
fr.add(p);
fr.setVisible(true);
当我运行这段代码,我得到以下异常(因为我点击按钮) / em>:
线程中的异常AWT-EventQueue-0java.lang.IllegalArgumentException:向容器添加一个窗口
这是什么意思,我该如何解决?
这是什么意思..
一个顶级容器(对话框)不能添加到另一个(框架)。
..我该如何解决? p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ <$ <$ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<对话框,而不是添加它。
我同意做Swing教程。你似乎正在把随机代码放在一起,希望它能奏效。
I have a
JDialog
class namedPreferences.java
. This class creates a constructor like:class Preferences extends javax.swing.JDialog { Preferences(java.awt.Frame parent,modal) { super(parent,modal); //...... } }
In my program i want this preferences dialog box to open up as i click a button from a Jframe form.After i registered the action listener on the button, i wrote the code inside as :
Frame fr = new Frame(); Preferences p = new Preferences(fr,false); fr.add(p); fr.setVisible(true);
When i run this code i get the following exception (as i click the button) :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
What does this mean and how can i solve this ?
解决方案What does this mean..
One top level container (dialog) cannot be added to another (frame).
..and how can i solve this ?
Just call
setVisible(true)
on thePreferences
dialog, rather than adding it.I agree with doing the Swing Tutorials. You seem to be throwing random code together, hoping it will work.
这篇关于例外:向容器添加一个窗口如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!