为什么我的 GUI 看起来总是不对? [英] Why does my GUI never look right?

查看:33
本文介绍了为什么我的 GUI 看起来总是不对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Java 在 Mac 上使用 Netbeans GUI 构建器构建.

Built using Netbeans GUI builder on a Mac in Java.

这是 Netbeans 中 GUI 的样子:

This is what the GUI looks like in Netbeans:

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

When I click preview, it doesn't look too bad, but there are small changes:

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

Lastly, when I run it, it looks like this: - terrible

我认为这与 Java 的外观和感觉"有关.我试过删除它,GUI 变得一团糟.那么我的选择是什么?正如您在 Netbeans 中看到的那样,当我运行它时,一切都是一团糟.

I assume it's to do with Java's 'Look and Feel'. I've tried removing this and the GUI becomes a shambles. So what are my options? As you can see everything lines up in Netbeans, when I run it, everything is a mess.

有什么想法吗?花了一天时间搞这个,我已经厌倦了至少可以说

Any ideas? Spent a day messing around with this and i'm fed up to say the least

推荐答案

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

Some where in you code, Netbeans would have created a main method (this will be within the class that Netbeans exeuctes and is probably your master frame - I think that would be the SPPMainGUI)

它看起来像这样(注意它可能被折叠",所以你只会看到第一条评论和第二条的decs(外观设置代码(可选))))

It will look something like this (note that it might be "folded" so you will only see the first comment and the decs of the second (Look and feel setting code (optional)))

/* 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());如下

Unfold the code block by click on the little + in the margin and replace the for-loop with UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); as below

/* 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 发现了这个问题,如果你能接受他的回答是正确的,我会很感激(而不是给我投票;),轻推,轻推,眨眼,眨眼,不要再说了)

As afsantos identified the problem, I would appriciate it if you could accept his answer as been correct (and an up-vote me instead ;), nudge, nudge, wink, wink, say no more)

这篇关于为什么我的 GUI 看起来总是不对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆