gpt4 book ai didi

java - UIManager.setLookAndFeel 用于嵌套类

转载 作者:行者123 更新时间:2023-12-02 09:08:03 25 4
gpt4 key购买 nike

是否可以设置外观一次并将其“级联”到所有嵌套类?

在下面的示例中,我在类 Test 中设置了外观,但是我添加到 MainPanel 类中的 JFileChooser (嵌套在我的 Test 类中)不会调整其外观和感觉,除非我在该类中再次设置它。

这只是我创建的每个类都需要做的事情吗?或者有没有办法让我在所有类中应用相同的外观和感觉?

import java.awt.Dimension;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
* Class to demonstrate UIManager.setLookAndFeel issue.
*/
public class Test {

/**
* Main program.
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}

/**
* Constructor.
*/
public Test() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}

createGUI();
}

/**
* Set up the JFrame and add the main JPanel.
*/
public void createGUI() {
JFrame frame = new JFrame("Test");

frame.add(new MainPanel(800, 600));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
frame.setLocationRelativeTo(null);
}

/**
* Class for the main panel that will hold all other components.
*/
class MainPanel extends JPanel {

private final int width;
private final int height;

/**
* Serialize/save.
*/
private static final long serialVersionUID = -3727866499459986351L;

/**
* Constructor.
*/
public MainPanel(int w, int h) {
this.width = w;
this.height = h;

// ISSUE the chooser does not have the look and feel of my OS
// unless I set the look and feel in this constructor
JFileChooser chooser = new JFileChooser();
this.add(chooser);
}

/**
*
*/
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
}

}

最佳答案

“问题”在这一行:

 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

您所看到的外观和感觉是跨平台的。这是 Java 的原始版本 - 哎呀。

尝试将其更改为:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

我确信它会被改变。

关于java - UIManager.setLookAndFeel 用于嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59647091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com