例外:向容器添加窗口。怎么解决? [英] Exception : adding a window to a container. How to solve it?

查看:525
本文介绍了例外:向容器添加窗口。怎么解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为偏好 JDialog 类。这个类创建一个构造函数,如:

I have a JDialog class named Preferences. This class creates a constructor like:

class Preferences extends javax.swing.JDialog {
          Preferences(java.awt.Frame parent,modal)  {
                      super(parent,modal);
                      //......
          }
}

在我的程序中,我希望在我从 JFrame 表单中单击按钮时打开此首选项对话框。在按钮上注册动作监听器之后,我在里面写了代码:

In my program I want this preferences dialog 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 it?

推荐答案


这是什么意思..

What does this mean..

一个顶部级别容器(对话框)无法添加到另一个(框架)。

One top level container (dialog) cannot be added to another (frame).


..我该如何解决这个问题?

..and how can i solve this ?

只需在首选项 setVisible(true) $ c>对话框,而不是添加它。

Just call setVisible(true) on the Preferences dialog, rather than adding it.

这篇关于例外:向容器添加窗口。怎么解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆