开发者

Java - setVisible(true) has no effect on GUI

I created a GUI (called ParameterUI) with the Netbeans GUI Builder and now I want to create an instance of it and display it. However, using

ParameterUI gui = new ParameterUI();
gui.setVisible(true);

doesn't cause any window to appear... Testing shows that after those commands, gui.isVisible() returns true, but gui.isValid() is false. Calling gui.revalidate() has no effect either.

In the ParameterUI class, the constructor method is generated by Netbeans and is simply

public class ParameterUI extends javax.swing.JPanel {
    public ParameterUI() {
        initComponents();
    }
}

initComponents is simply a listing of where each jPanel etc. will be placed.

The strange thing is that when I made a practice GUI with the tutorial at http://netbeans.org/kb/docs/java/gui-functionality.html , the GUI was set as t开发者_StackOverflow社区he main class despite having no main method and the GUI appeared of its own accord.

Unfortunately I'm a novice with GUIs (I'm using the builder cause I haven't got time to learn how to make a proper hand-made GUI), but can someone tell me how to make my GUI visible? I can provide more code if necessary...

EDIT: I tried

JFrame window = new JFrame();
ParameterUI gui = new ParameterUI();
window.setContentPane(gui);
window.pack();
window.setVisible(true);

having read a short tutorial on JFrames, but it doesn't seem to change anything...


setVisible() on a Component sets a flag in that component (among other things you don't really care about at this point). This flag is checked by the container that contains your component to see whether the component needs to be shown.

setVisible() on a Window controls whether the window is displayed on the screen. Now, all it does is make your window appear or disappear. Typically, you want to given it some size and location before making it visible. pack() and setLocationRelativeTo() are useful here.

So to see your gui, ParameterUI either has to extend Window (probably JFrame or JDialog) or it has to be contained in a window and you should call setVisible(true) on the window instead of the ParameterUI instance. A simple example of doing so is (untested):

// expected to be called on the AWT/Event Dispatch Thread
public void show(ParameterUI ui) {
  JFrame frame = new JFrame();
  frame.setLayout(new BorderLayout());
  frame.add(ui, BorderLayout.CENTER);
  frame.pack();
  frame.setLocationRelativeTo(null); // position in the center of the screen
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.setVisible(true);
}


Are you using a JFrame or did you create a Desktop application with Netbeans? Because if you created a desktop application, Netbeans has its own class that it uses and I have had many problems with it as well... thus, I suggest you use a JFrame. Any how, you can try this to see if the UI launches:

SwingUtilities.invokeLater(new Runnable() {
           public void run()
           {
               ParameterUI gui = new ParameterUI();
               gui.setVisible(true);
           }
       });

Since you are extending the JPanel, you will need to put your panel onto a JFrame to be visible. To do this, in netbeans, simply create a new JFrame (right click on the package and select "New JFrame". Now, go back to your panel, on your left margin (under Project, Files, etc) you should have an item named "Inspector" Click that and you should see a tree view of your components. Right click on the JPanels you want to have visible and select "Copy". Go back to the JFrame that has just been created, find the "Inspector" button from the left margin, click it and on top you should have an item named "[JFrame]". Right click on that item and select paste. You should now see the panel you have created.

To view the panel then simply put the name of the JFrame instead of ParameterUI

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜