java - 为什么我的 GUI 看起来总是不正确?

标签 java user-interface netbeans look-and-feel

在 Mac 上使用 Java 中的 Netbeans GUI 构建器构建。

这就是 Netbeans 中 GUI 的样子:

Netbeans GUI

当我点击预览时,看起来还不错,但有一些小变化:

GUI Preview

最后,当我运行它时,它看起来像这样: - 糟糕

Look and feel

我认为这与 Java 的“外观和感觉”有关。我尝试删除它,但 GUI 变得一团糟。那么我有什么选择呢?正如您所看到的,Netbeans 中的所有内容都排列整齐,当我运行它时,一切都一团糟。

有什么想法吗?花了一天时间搞这个,至少可以说我受够了

最佳答案

在您的代码中,Netbeans 会创建一个 main 方法(这将位于 Netbeans 执行的类中,并且可能是您的主框架 - 我认为这将是 SPPMainGUI )

它看起来像这样(请注意,它可能被“折叠”,因此您只能看到第一个注释和第二个注释的 decs (外观设置代码(可选)))

/* 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>

通过单击边距中的小+展开代码块,并将for-loop替换为UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());,如下所示

/* 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 {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 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>

当 afsantos 发现了这个问题时,如果你能接受他的答案是正确的(并且给我投赞成票;),轻推,轻推,眨眼,眨眼,别再说了,我会很感激。

关于java - 为什么我的 GUI 看起来总是不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14968806/

相关文章:

java - 如何从 JFrame 中删除 JscrollPane?

c++ - Netbeans 8.1(用于 C/C++)找不到我的编译器(gcc-6.0.0 开发版)

java - 配置 Jackson 而不是使用 DateTimeFormat.ISO.DATE - 不工作

ios - 如何在 MapKit 上覆盖搜索栏?

java - 条件的 XPath 语法

eclipse - 将 window tester 与 eclipse 集成以测试 UI

selenium - 使用 Protractor 定位链接的最佳方法

java - 从哪里获得 com.sun.j3d 类

java - 如果列表为空/空,则 IN 子句选择所有元素

java - 无法将 ImageJ 集成到 Android 应用程序中