在 Mac 上使用 Java 中的 Netbeans GUI 构建器构建。
这就是 Netbeans 中 GUI 的样子:
当我点击预览时,看起来还不错,但有一些小变化:
最后,当我运行它时,它看起来像这样: - 糟糕
我认为这与 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/