表单布局问题 - Java Swing GUI(Netbeans)

时间:2016-01-11 00:09:36

标签: java swing user-interface netbeans

运行时我的应用程序设计存在此问题。下图显示了我希望我的应用程序看起来如何在netbeans的设计视图中创建。

Form Editor

以下是应用程序运行时的外观。

GUI when running.

边框甚至与文本对齐,按钮组件和滚轮与设计视图中的不匹配。有没有一种方法可以将这个主题应用于我正在运行的应用程序,因为GUI设计很可怕。

1 个答案:

答案 0 :(得分:2)

布局没有任何问题,只是表单设计器显示系统的外观和感觉,并且你正在运行Nimbus。请查看How to Set the Look and Feel了解更多详情

在你的“主”类/方法中,你会得到一些看起来像......

的代码
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
 */
try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

可以简单地替换为......

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    ex.printStackTrace();
}

将导致Swing使用系统/本机外观而不是