Java - setVisible(true) 对 GUI 没有影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3433809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - setVisible(true) has no effect on GUI
提问by Stella
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
我使用 Netbeans GUI Builder 创建了一个 GUI(称为 ParameterUI),现在我想创建它的一个实例并显示它。但是,使用
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.
不会导致任何窗口出现...测试表明,在这些命令之后,gui.isVisible() 返回 true,但 gui.isValid() 为 false。调用 gui.revalidate() 也没有效果。
In the ParameterUI class, the constructor method is generated by Netbeans and is simply
在ParameterUI类中,构造方法由Netbeans生成,简单来说就是
public class ParameterUI extends javax.swing.JPanel {
public ParameterUI() {
initComponents();
}
}
initComponents is simply a listing of where each jPanel etc. will be placed.
initComponents 只是一个列表,列出了每个 jPanel 等的放置位置。
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.
奇怪的是,当我使用http://netbeans.org/kb/docs/java/gui-functionality.html上的教程制作了一个练习 GUI 时,尽管没有 main 方法和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...
不幸的是,我是 GUI 的新手(我正在使用构建器,因为我没有时间学习如何制作适当的手工制作的 GUI),但是有人能告诉我如何使我的 GUI 可见吗?如有必要,我可以提供更多代码...
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...
阅读了关于 JFrames 的简短教程,但它似乎没有改变任何东西......
采纳答案by npinti
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:
您使用的是 JFrame 还是使用 Netbeans 创建了桌面应用程序?因为如果你创建了一个桌面应用程序,Netbeans 有它自己使用的类,我也有很多问题......因此,我建议你使用 JFrame。不管怎样,你可以试试这个,看看 UI 是否启动:
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.
由于您要扩展 JPanel,因此您需要将面板放在 JFrame 上以使其可见。为此,在 netbeans 中,只需创建一个新的 JFrame(右键单击包并选择“New JFrame”。现在,返回到您的面板,在您的左边距(在项目、文件等下)您应该有一个项目名为“Inspector”单击它,您应该会看到组件的树视图。右键单击要显示的 JPanels 并选择“Copy”。返回刚刚创建的 JFrame,找到“Inspector”按钮从左边距,单击它,顶部应该有一个名为“[JFrame]”的项目。右键单击该项目并选择粘贴。您现在应该看到您创建的面板。
To view the panel then simply put the name of the JFrame instead of ParameterUI
要查看面板,只需输入 JFrame 的名称而不是 ParameterUI
回答by Hymanrabbit
setVisible()
on a Componentsets 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 Component在该组件中设置一个标志(除其他事项外,您此时并不真正关心)。该标志由包含您的组件的容器检查,以查看是否需要显示该组件。
setVisible()
on a Windowcontrols 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.
setVisible()
on a Window控制窗口是否显示在屏幕上。现在,它所做的就是让您的窗口出现或消失。通常,您希望在使其可见之前为其指定一些大小和位置。pack()
并且setLocationRelativeTo()
在这里很有用。
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):
因此,要查看您的 gui,ParameterUI
要么必须扩展Window
(可能JFrame
或JDialog
),要么必须将其包含在窗口中,并且您应该调用setVisible(true)
窗口而不是ParameterUI
实例。这样做的一个简单示例是(未经测试):
// 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);
}