2015-10-18 82 views
0

比方说,我有2个JFrames。将JFrame设置为可见之后,如果不首先将隐藏和正常的可见性设置为可见,您似乎无法更改LAF。这意味着一个闪烁,并且JFrame跳转到顶部。如何让某个JFrame与其他JFrame有不同的LAF?

否则,你会得到这样的:

另外,重绘似乎并没有帮助(金属和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?

+0

* “比方说,我有2个JFrames。” *请参阅(HTTP [多个JFrames,好/坏的做法的使用?] ://stackoverflow.com/q/9554636/418556) –

回答

1

它看起来好像是在创建JFrames时应用的,因此只需在创建下一个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); 

制作:

enter image description here