Swing:Look And FeelAdvertisementLook And Feel
SwingはLook And Feel(以下L&F)というプラグインを使ってGUIのスタイルを設定することができる。自分でL&Fを作成することも可能。
現在設定されているL&Fを表示。
import javax.swing.*;
public class NowLookAndFeel{
public static void main(String args[]){
System.out.println(UIManager.getLookAndFeel());
}
}
結果
[The Java(tm) Look and Feel - javax.swing.plaf.metal.MetalLookAndFeel]現在はMetalLookAndFeelを使用していることがわかる。 使用可能なL&Fを表示。
import javax.swing.*;
public class UseLookAndFeel{
public static void main(String args[]){
UIManager.LookAndFeelInfo[] lfinfo
= UIManager.getInstalledLookAndFeels();
for(int i = 0; i < lfinfo.length; i++){
System.out.println(lfinfo[i].getClassName());
}
}
}
結果
javax.swing.plaf.metal.MetalLookAndFeel com.sun.java.swing.plaf.motif.MotifLookAndFeel com.sun.java.swing.plaf.windows.WindowsLookAndFeel L&F の設定。
setLookAndFeel() メソッドで変更して、updateComponentTreeUI() メソッドを実行。
例
try{
UIManager.setLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel);
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception ex){
System.out.println("Error L&F Setting");
}
現在のL&Fを取得するときは getLookAndFeel() メソッド。
サンプル
import javax.swing.*;
import java.awt.event.*;
public class LfChange{
public static void main(String args[]){
MyFrame mf = new MyFrame();
mf.setSize(300, 200);
mf.setTitle("Look & Feel");
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.show();
}
}
class MyFrame extends JFrame{
UIManager.LookAndFeelInfo[] lfInfo;
JButton changeButton;
JTextPane text;
JScrollPane scroll;
JMenuBar menuBar;
JMenu file;
JMenuItem quit;
int count = 1;
public MyFrame(){
lfInfo = UIManager.getInstalledLookAndFeels();
text = new JTextPane();
scroll = new JScrollPane(text);
changeButton = new JButton("Change");
changeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){changeLF();}
});
getContentPane().add(scroll, "Center");
getContentPane().add(changeButton, "South");
setJMenuBar(createMenu());
}
public void changeLF(){
try{
UIManager.setLookAndFeel(lfInfo[count].getClassName());
SwingUtilities.updateComponentTreeUI(this);
count = count + 1;
if(count >= lfInfo.length){
count = 0;
}
}catch(Exception ex){
System.out.println("Error L&F Setting");
}
}
//メニュー作り
public JMenuBar createMenu(){
menuBar = new JMenuBar();
file = new JMenu("ファイル");
quit = new JMenuItem("終了");
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){System.exit(0);}
});
file.add(quit);
menuBar.add(file);
return menuBar;
}
}
![]() Change ボタンで L&F 変更。 Advertisement |
ショートカット・634・634ブログ ・このカテゴリのトップページに戻る ・Incubator(Pukiwiki) ・634ラボ UIコレクションギャラリー ZO-3ジェネレーター サイト検索Y!ログールビリヤード |