Tips/LookAndFeelDebugAgent のバックアップ(No.5)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Tips/LookAndFeelDebugAgent へ行く。
- 1 (2011-01-26 (水) 17:49:53)
- 2 (2013-10-31 (木) 17:00:38)
- 3 (2014-09-03 (水) 18:19:49)
- 4 (2014-11-08 (土) 01:41:12)
- 5 (2015-03-27 (金) 21:45:05)
- 6 (2017-10-27 (金) 16:26:13)
- 7 (2018-05-30 (水) 21:02:06)
- 8 (2018-10-12 (金) 17:40:15)
- 9 (2025-01-03 (金) 08:57:02)
- 10 (2025-01-03 (金) 09:02:38)
- 11 (2025-01-03 (金) 09:03:21)
- 12 (2025-01-03 (金) 09:04:02)
- 13 (2025-01-19 (日) 09:59:04)
- title: SwingアプリケーションのLookAndFeelを外部から切り替える author: aterai pubdate: 2008-11-28 description: SwingアプリケーションのLookAndFeelをagentを使って外部から切り替えてデバッグします。
概要
Swing
アプリケーションのLookAndFeel
をagent
を使って外部から切り替えてデバッグします。
ソースコード
package swinghelper;
import java.awt.*;
import java.awt.event.*;
import java.lang.instrument.*;
import javax.swing.*;
public class LookAndFeelDebugAgent {
public static void premain(String args) throws Exception {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
ButtonGroup group = new ButtonGroup();
Box box = Box.createVerticalBox();
for(LookAndFeelEnum lnf : LookAndFeelEnum.values()) {
JRadioButton rb = new JRadioButton(new ChangeLookAndFeelAction(lnf));
group.add(rb); box.add(rb);
}
box.add(Box.createVerticalGlue());
box.setBorder(BorderFactory.createEmptyBorder(5,25,5,25));
JFrame frame = new JFrame("LnF");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.getContentPane().add(box);
frame.pack();
frame.setVisible(true);
}
private static enum LookAndFeelEnum {
Metal ("javax.swing.plaf.metal.MetalLookAndFeel"),
Mac ("com.sun.java.swing.plaf.mac.MacLookAndFeel"),
Motif ("com.sun.java.swing.plaf.motif.MotifLookAndFeel"),
Windows("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"),
GTK ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"),
Nimbus ("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
private final String clazz;
private LookAndFeelEnum(String clazz) {
this.clazz = clazz;
}
public String getClassName() {
return clazz;
}
}
private static class ChangeLookAndFeelAction extends AbstractAction{
private final String lnf;
protected ChangeLookAndFeelAction(LookAndFeelEnum lnfe) {
super(lnfe.toString());
this.lnf = lnfe.getClassName();
this.setEnabled(isAvailableLookAndFeel(lnf));
}
private static boolean isAvailableLookAndFeel(String lnf) {
try{
Class lnfClass = Class.forName(lnf);
LookAndFeel newLnF = (LookAndFeel)(lnfClass.newInstance());
return newLnF.isSupportedLookAndFeel();
}catch(Exception e) {
return false;
}
}
@Override public void actionPerformed(ActionEvent e) {
try{
UIManager.setLookAndFeel(lnf);
}catch(Exception ex) {
ex.printStackTrace();
System.out.println("Failed loading L&F: " + lnf);
}
for(Frame f:Frame.getFrames()) {
if(f instanceof JFrame) {
SwingUtilities.updateComponentTreeUI(f);
f.pack();
}
}
}
}
}
解説
Look And Feel
を切り替えるためのパネルが一緒に起動し、対象アプリケーションのソースコードを変更することなく、LookAndFeel
の変更をテストすることが出来ます。
javaagent
を使うので、JDK 1.6
以上で以下のように起動します。
java -javaagent:lnfagent.jar -jar example.jar