Java - ルック・アンド・フィール
アプリケーションの見栄えを良くするために、Swingアーキテクチャは、各コンポーネントをJComponent とComponentUIという2つの異なるクラスに分離することで、アプリケーションまたはGUIのルック・アンド・フィールを変更するように作られています。たとえば、JListの各インスタンスには、ComponentUI.スーパークラスを継承する具体的なListUI実装があり、次のメソッドを使用します。UIManager.setLookAndFeel(LookAndFeel l ) を引数として LookAndFeel のクラス名を指定します.
変更後、メソッドを呼び出してコンポーネントツリーを更新する必要があります SwingUtilities.updateComponentTreeUI(frame);
Example
この例では、Metal、System、Pattern、GTK のいずれかの有効な値を含む LOOKANDFEEL 定数を宣言しています。このコードは Eclipse と Netbeans.
import javax.swing.*;Output:
import java.awt.*;
public class LookAndFeelTest extends JFrame{
public LookAndFeelTest(){
//LookAndFeel
初期化 initLookAndFeel();
//学習が適用されていることを確認します
JFrame.setDefaultLookAndFeelDecorated(true);
//ウィンドウを作成して構成します
JFrame frame = new JFrame("SwingApp");
コンポーネントの内容 = createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingUtilities.updateComponentTreeUI(frame);
//ウィンドウを表示
frame.pack();
frame.setVisible(true);
}
/*
*
* LOOKANDFEEL 定数を設定して、使用する外観を指定します
*/
final static String LOOKANDFEEL = "Metal";
public Component createComponents() {
JButton button = new JButton("Button");
JLabel label = new JLabel(LOOKANDFEEL);
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(button);
panel.add(ラベル);
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
リターンパネル。
}
private static void initLookAndFeel() {
String lookAndFeel = null;
if(LOOKANDFEEL != null) {
switch(LOOKANDFEEL){
"Metal" box:lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
estate;
"System":lookAndFeel = UIManager.getSystemLookAndFeelClassName();
estate;
"パターン":lookAndFeel = "com.sun.java.swing.plaf.pattern.PatternLookAndFeel";
estate;
box "GTK":lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
estate;
}
try {
UIManager.setLookAndFeel(lookAndFeel);
}
catch (例外 e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new LookAndFeelTest();
}
}
System
Windows 7.
lookAndFeel = UIManager.getSystemLookAndFeelClassName();Out:
Motif
lookAndFeel="com.sun.java.swing.plaf.motif.MotifLookAndFeel";Out:
References:
Oracle Documentation: ルック・アンド・フィールの設定方法
Developpez.net: Java GUI:Look &フィール