Java – exception: add a window to the container How?

I have a jdialog class called preferences This class creates a constructor, such as:

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 when I click a button in the JFrame form After registering the action listener on the button, I write the code to:

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 (when 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?

Solution

A top-level container (dialog box) cannot be added to another (frame)

Simply call setVisible (true) in the preferences dialog box instead of adding it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>