Java - setVisible(true)对GUI没有影响 [英] Java - setVisible(true) has no effect on GUI

查看:142
本文介绍了Java - setVisible(true)对GUI没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Netbeans GUI Builder创建了一个GUI(名为ParameterUI),现在我想创建它的一个实例并显示它。但是,使用

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);

不会导致出现任何窗口...
测试显示在这些命令之后,gui.isVisible()返回true,但gui.isValid()为false。调用gui.revalidate()也没有效果。

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.

在ParameterUI类中,构造函数方法由Netbeans生成,只是

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只是每个jPanel等的列表。将被放置。

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

奇怪的是,当我使用 http://netbeans.org/kb/docs/java/gui-functionality.html ,尽管已经将GUI设置为主类没有主要的方法和GUI自己出现。

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 the main class despite having no main method and the GUI appeared of its own accord.

不幸的是我是GUI的新手(我正在使用构建器因为我没有时间学习如何制作一个合适的手工制作的GUI),但有人能告诉我如何让我的GUI可见吗?如有必要,我可以提供更多代码......

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...

编辑:我试过

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

阅读了关于JFrames的简短教程,但它似乎没有改变任何东西......

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

推荐答案

您使用的是JFrame还是使用Netbeans创建了桌面应用程序?因为如果你创建了一个桌面应用程序,Netbeans有它自己的类,我也有很多问题...因此,我建议你使用JFrame。如何,您可以尝试这个以查看UI是否启动:

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

由于您要扩展JPanel,因此您需要将面板放到JFrame上才能看到。要做到这一点,在netbeans中,只需创建一个新的JFrame(右键单击包并选择New JFrame。现在,回到你的面板,在你的左边缘(在Project,Files等)你应该有一个项目命名为Inspector单击它,您应该看到组件的树视图。右键单击要显示的JPanels并选择Copy。返回刚刚创建的JFrame,找到Inspector按钮从左边距,单击它,在顶部你应该有一个名为[JFrame]的项目。右键单击该项目并选择粘贴。你现在应该看到你创建的面板。

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.

要查看面板,只需输入JFrame的名称而不是ParameterUI

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

这篇关于Java - setVisible(true)对GUI没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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