java - UIManager.setLookAndFeel 用于嵌套类

标签 java swing

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

在下面的示例中,我在类 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/

相关文章:

java - 按升序对数组元素进行排序

java - equals 和 hashcode - 证明合约合理

java - 如何将一种方法的结果作为另一种方法的参数

java - 如何在JFrame上用java绘制1024交叉1024网格单元?

java - 采用 Swing 对话框方法并将其转换为 SWT

java - 带填充的 XSL 左右对齐

java - 如何在 Java POJO 中使用 @OPTIONS 注释调用 JAX-RS Web 服务进行模型化

java - 自动调整 1 列的大小,留下另一列占用剩余的可用空间。在 JScrollPane 中使用 JTable

java - 浏览器不会显示 HTML 文件

java - 列太小的 TableCellRenderer 没有点后缀