目次

ルック・アンド・フィール

  • Swingでは、GUI部品の見栄えを表すルック・アンド・フィール(look and feel)をダイナミックに変更することができる。

サンプル

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
-
|
!
-
|
|
|
|
|
|
!
-
-
|
|
-
|
-
|
|
!
!
|
-
|
-
|
!
!
|
-
|
-
|
!
!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
-
-
|
-
|
-
|
-
|
-
|
!
|
!
!
!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
 
 
public class LookAndFeel {
 
    public static void main(String[] args) {
        new LookAndFeel();
    }
    private LookAndFeel() {
        JFrame frame = new JFrame("Test");
        LookAndFeelPanel panel = new LookAndFeelPanel();
        frame.getContentPane().add(panel);
        frame.setSize(350, 100);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private class LookAndFeelPanel extends JPanel {
        public LookAndFeelPanel() {
            JLabel label = new JLabel("ルック・アンド・フィール", SwingConstants.CENTER);
            JButton metalButton = new JButton("Metal");
            metalButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent event) {
                    // "com.sun."は付かないことに注意.
                    updateLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                }
            });
            JButton motifButton = new JButton("Motif");
            motifButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent event) {
                    updateLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                }
            });
            JButton windowsButton = new JButton("Windows");
            windowsButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent event) {
                    updateLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                }
            });
 
            setBackground(Color.white);
            setLayout(new GridLayout(2, 1));
            JPanel p1 = new JPanel();
            JPanel p2 = new JPanel();
 
            p1.setLayout(new GridLayout(1, 3, 20, 0));
            p1.add(metalButton);
            p1.add(motifButton);
            p1.add(windowsButton);
 
            p2.setLayout(new GridLayout(1, 1));
            p2.add(label);
 
            add(p1);
            add(p2);
        }
        private void updateLookAndFeel(final String className) {
            try {
                UIManager.setLookAndFeel(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
            SwingUtilities.updateComponentTreeUI(LookAndFeelPanel.this);
        }
    }
}

Metal

Motif

Windows

参考文献

  • 『I/O BOOKS Java Swing教科書』