首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套类的UIManager.setLookAndFeel

嵌套类的UIManager.setLookAndFeel
EN

Stack Overflow用户
提问于 2020-01-08 13:37:01
回答 1查看 154关注 0票数 0

是否有可能将外观和感觉一次性设置为,并将其“级联”到所有嵌套类?

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

这仅仅是我创建的每一个类都需要做的事情吗?还是有一种方法可以让我在所有的课程中都有相同的外观和感觉?

代码语言:javascript
复制
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);
        }
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-08 13:46:46

“问题”就在这一行:

代码语言:javascript
复制
 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

你的眼神和感觉你看到了它的交叉平台。这是爪哇的原版。

尝试将其更改为:

代码语言:javascript
复制
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

我相信它会被改变的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59647091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档