【问题标题】:Can't call UI classes with netbeans GUI builder无法使用 netbeans GUI builder 调用 UI 类
【发布时间】:2014-03-17 07:24:49
【问题描述】:

正在学习 netbeans 的 GUI 构建器,我很困惑。我正在使用 GUI 构建器编写一个基本的 LoginUI 类。如果我以独立方式运行该类,我想要的窗口会按预期弹出。但是,如果我尝试创建它的新实例,它不会。我的控制流程如下:

public static void main(String[] args) {
    LoginCntl theLoginCntl = new LoginCntl();
}

public class LoginCntl {

    public LoginCntl(){
        LoginUI theLoginUI = new LoginUI();
}

}

public LoginUI() {
    initComponents();
    System.out.println("Are we here?");
    //This prints out, so I know the program gets here. The problem is the window does not show up here. 
}

 java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new LoginUI().setVisible(true);
        }
    });

我真的不知道为什么我可以独立运行它并获得我想要的窗口,然后当我创建它的新实例时,窗口不会出现。我觉得我错过了一些非常基本的东西。任何帮助将不胜感激。

编辑:添加了更多 netbeans 提供的生成代码。它确实调用了 setVisible()。话虽如此,在寻找 setVisible() 时,我注意到 GUI builder 添加了

public static void main(String args[]) {

在其生成的一些代码中。据我所知,您应该在一个项目中只拥有 1 个主类。这可能是我的问题吗?

【问题讨论】:

  • theLoginUI.setVisible(true)
  • 感谢您的帮助,setvisible 工作。我看到构建器在异常处理程序中调用了 setVisible,所以我认为它会为我处理好这个问题。显然它没有。你知道的越多!

标签: java user-interface netbeans


【解决方案1】:

您永远不会在任何地方调用 set visible,事实上,无法确定您的 UI 是否真的有要显示的窗口。

如果 LoginUI 扩展自 JFrame 之类的东西,您应该调用...

theLoginUI.pack();
theLoginUI.setVisible(true);

【讨论】:

  • 其实 pack() 已经在 initComponents() 中调用了 setVisible(true),你赢了我 xD 时间 +1
  • @nachokk 假设组件从 JFrame 扩展,并且 NetBeans 已配置为自动应用包,但就个人而言,永远不要冒险;)
猜你喜欢
  • 2012-07-26
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2011-05-29
  • 2019-01-27
  • 2012-11-02
相关资源
最近更新 更多