[英]Regarding the Look and Feel in Java
在Java中,我想设置程序的外观(特别是“获取系统外观”)。
是否需要在每个组件(即每个JPanel,每个JFrame)中设置外观?
还是只设置一次外观就足以影响整个应用程序?
如果一次就够了,那么我应该在哪里放置代码(在什么类中?)
您可以在main方法中定义它,并且该方法将在任何地方使用。
这是主屏幕的主要方法:
例:
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainScreen mainWindow = new MainScreen();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
mainWindow.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
还是只设置一次外观就足以影响整个应用程序?
正确
如果一次就够了,那么我应该在哪里放置代码(在什么类中?)
通常,在创建应用程序框架的类中(在创建之前):
UIManager.setLookAndFeel(lookAndFeelName);
...
JFrame frame = new JFrame(...);
另外,如果需要,您可以在整个应用程序中即时更改LnF:
UIManager.setLookAndFeel(lookAndFeelName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.