[英]Why does my GUI never look right?
Built using Netbeans GUI builder on a Mac in Java. 使用Java中的Mac上的Netbeans GUI构建器构建。
This is what the GUI looks like in Netbeans: 这就是Netbeans中GUI的样子:
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 最后,当我运行它时,它看起来像这样: - 太可怕了
I assume it's to do with Java's 'Look and Feel'. 我认为这与Java的“外观和感觉”有关。 I've tried removing this and the GUI becomes a shambles.
我试过删除它,GUI变得一团糟。 So what are my options?
那么我的选择是什么? As you can see everything lines up in Netbeans, when I run it, everything is a mess.
正如你可以看到Netbeans中的所有内容一样,当我运行它时,一切都很糟糕。
Any ideas? 有任何想法吗? Spent a day messing around with this and i'm fed up to say the least
花了一天时间搞乱了这一点,我至少可以说
From the looks of it, the preview in Netbeans is using a different Look and Feel from the one Java is using when you run the application. 从它的外观来看,Netbeans中的预览使用的是与Java运行应用程序时使用的外观不同的外观 。
However, you may try to manually set the Look and Feel you want. 但是,您可以尝试手动设置所需的外观 。 There is a tutorial from Oracle, on how to get and set the Look and Feel .
Oracle提供了一个关于如何获取和设置外观的教程 。
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
) 在你编码的某些地方,Netbeans会创建一个
main
方法(这将在Netbeans执行的类中,并且可能是你的主框架 - 我认为这将是SPPMainGUI
)
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)
)) 它看起来像这样(注意它可能是“折叠”的,所以你只会看到第一个注释和第二个的
decs
( 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>
Unfold the code block by click on the little + in the margin and replace the for-loop
with UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
通过单击边距中的小+展开代码块,并用
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
替换for-loop
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>
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) 当afsantos发现问题时,如果你能接受他的答案是正确的(并且反而投票给我),我会赞美它,轻推,轻推,眨眼,眨眼,不再说了)
As you mentioned, it has to do with the Look and Feel, so really, the only issue if finding what L&F Netbeans uses by default. 正如你所提到的,它与外观和感觉有关,所以真的,如果找到L&F Netbeans默认使用的唯一问题。 A Google brought up this page .
谷歌提出了这个页面 。 I quote,
我引用,
In NetBeans 7 the default look and feel is automatically set to Nimbus look and feel.
在NetBeans 7中,默认外观自动设置为Nimbus外观。
Nimbus is a polished cross-platform look and feel introduced in the Java SE 6 Update 10 (6u10) release.
Nimbus是Java SE 6 Update 10(6u10)版本中引入的优质跨平台外观。
To enable the Nimbus Look and Feel, simply add these self-explanatory lines somewhere: 要启用Nimbus外观,只需在某处添加这些不言自明的线条:
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.