import javax.swing.*; import java.awt.event.*; public class HelloWorldSwingLookAndFeel extends JFrame implements ActionListener { // コンストラクタ public HelloWorldSwingLookAndFeel(String title) { super(title); // ボタンを作成 JButton button = new JButton("Hello World"); // チェックボックスを作成 /** Metal **/ JRadioButton metalButton = new JRadioButton("Metal"); // MetalのLook&Feelのクラス名を設定 metalButton.setActionCommand( "javax.swing.plaf.metal.MetalLookAndFeel"); /** Windows **/ JRadioButton windowsButton = new JRadioButton("Windows"); // WindowsのLook&Feelのクラス名を設定 windowsButton.setActionCommand( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); /** Motif **/ JRadioButton motifButton = new JRadioButton("Motif"); // MotifのLook&Feelのクラス名を設定 motifButton.setActionCommand( "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); // アクションリスナーを登録 metalButton.addActionListener(this); windowsButton.addActionListener(this); motifButton.addActionListener(this); // チェックボックスグループを作成 ButtonGroup group = new ButtonGroup(); group.add(metalButton); group.add(windowsButton); group.add(motifButton); // パネルを作成 JPanel panel = new JPanel(); panel.add(button); panel.add(metalButton); panel.add(windowsButton); panel.add(motifButton); // コンテントペインにパネルを追加 getContentPane().add(panel); // ウインドウが閉じられたときにアプリケーションを終了するように設定 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // サブコンポーネントの推奨サイズおよびレイアウトに合わせて // この Window をサイズ変更するように設定 pack(); // 表示します setVisible(true); } public static void main(String[] args) { HelloWorldSwingLookAndFeel frame = new HelloWorldSwingLookAndFeel("SwingでLook&Feel"); } public void actionPerformed(ActionEvent ae) { try { // 外観を設定します UIManager.setLookAndFeel(ae.getActionCommand()); // 外観を変更します SwingUtilities.updateComponentTreeUI(this); // サブコンポーネントの推奨サイズおよびレイアウトに合わせて // この Window をサイズ変更するように設定 pack(); } catch (Exception e) { e.printStackTrace(); } } }