2014-03-04 96 views
1

在Java中,我想設置程序的外觀(特別是'get系統的外觀和感覺')。關於Java中的外觀和感覺

是否有必要在每個組件(也就是每個JPanel,每個JFrame)中設置外觀?

或者是設置外觀一次,足以影響整個應用程序?

如果一次就夠了,那麼我應該在哪裏放置代碼這樣做(以什麼課?)

回答

3

您在main方法定義它,它就會到處被使用。

這是主屏幕的主要方法:

例子:

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       MainScreen mainWindow = new MainScreen(); 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       mainWindow.frame.setVisible(true); 
       } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
+0

在'main'方法中,還是在生成主JFrame的類中? –

+0

查看更新的答案。 – Henrik

+0

好的,是的,在創建主JFrame的類中。謝謝:) –

1

或者是設置外觀和感覺一次,足以影響整個應用程序?

正確

如果一次就夠了,那麼我應該在哪裏放置代碼這樣做(以什麼課?)

通常情況下,在創建應用程序框架類(在它被創建之前):

UIManager.setLookAndFeel(lookAndFeelName); 
    ... 
    JFrame frame = new JFrame(...); 

此外,您可以更改整個應用程序的LnF,如果您需要d:

UIManager.setLookAndFeel(lookAndFeelName); 
SwingUtilities.updateComponentTreeUI(frame); 
frame.pack();