Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

java - Exception : adding a window to a container. How to solve it?

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);
                      //......
          }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 the Preferences dialog, rather than adding it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...