简体   繁体   中英

How can I make a certain JFrame have a different LAF from other JFrames?

Let's say I have 2 JFrames. After setting JFrame to visible, you cannot seem to change the LAF without setting the visibility to hidden and normal first. This means a flicker, and the JFrame jumping to the top.

Otherwise, you get this:

(Some mixture between Metal and the Ubuntu system LAF)

Also, repainting doesn't seem to help.

Now, let's say the first JFrame was made using the Metal LAF. After it is displayed, the LAF is changed to the system LAF. Then the second one appears. Now, the first one is affected by this funny thing. The second is OK. Why's this so? This is the code I have:

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);
}

So how can I make the new LAF only affect 1 JFrame?

Or is the LAF global and cannot be applied to only 1 JFrame?

It looks to me as though they are applied when you create the JFrames, so just change the setting before creating the next JFrame.

eg:

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);

Produced:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM