【问题标题】:How can I make a certain JFrame have a different LAF from other JFrames?如何使某个 JFrame 具有与其他 JFrame 不同的 LAF?
【发布时间】:2015-10-18 14:32:04
【问题描述】:

假设我有 2 个 JFrame。将 JFrame 设置为可见后,如果不先将可见性设置为隐藏和正常,您似乎无法更改 LAF。这意味着闪烁,JFrame 跳到顶部。

否则,你会得到这个:

(Metal 和 Ubuntu 系统 LAF 之间的某种混合)

另外,重绘似乎也无济于事。

现在,假设第一个 JFrame 是使用 Metal LAF 制作的。显示后,LAF 更改为系统 LAF。然后出现第二个。现在,第一个受到这个有趣的事情的影响。第二个没问题。为什么会这样?这是我的代码:

public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
    JFrame first = new JFrame("First");
    first.add(new JButton("Click me, I look funny"));
    first.setPreferredSize(new Dimension(100, 100));
    first.pack();
    first.setVisible(true);

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    first.repaint();

    JFrame second = new JFrame("Second");
    second.add(new JButton("Click"));
    second.setPreferredSize(new Dimension(100, 100));
    second.pack();
    second.setVisible(true);
    second.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

那么我怎样才能让新的 LAF 只影响 1 个 JFrame?

或者LAF是全局的,不能只应用于1个JFrame?

【问题讨论】:

标签: java swing look-and-feel uimanager


【解决方案1】:

在我看来它们好像是在您创建 JFrame 时应用的,所以只需在创建下一个 JFrame 之前更改设置。

例如:

UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
JFrame first = new JFrame("First");
first.add(new JButton("Click me, I look funny"));
first.setPreferredSize(new Dimension(300, 100));
first.pack();
first.setVisible(true);

UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

JFrame second = new JFrame("Second");
second.add(new JButton("Click"));
second.setPreferredSize(new Dimension(100, 100));
second.setLocation(300, 0);
second.pack();
second.setVisible(true);
second.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

制作:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多