Swingexamples-change-look-and-feel-of-window

提供:Dev Guides
移動先:案内検索

Swingの例-ウィンドウのルックアンドフィールを変更する

次の例は、Swingベースのアプリケーションでウィンドウのルックアンドフィールを変更する方法を示しています。

次のAPIを使用しています。

  • * UIManager.getCrossPlatformLookAndFeelClassName()*-Javaのルックアンドフィールを取得します。
  • * UIManager.setLookAndFeel()*-UIコンポーネントのルックアンドフィールを設定します。
  • JFrame.setDefaultLookAndFeelDecorated(true); -フレームの外観を変更します。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class SwingTester {
   public static void main(String[] args) {
      try {
         UIManager.setLookAndFeel(
         UIManager.getCrossPlatformLookAndFeelClassName());
      } catch (Exception e) {
   }
      JFrame.setDefaultLookAndFeelDecorated(true);
      createWindow();
   }

   private static void createWindow() {
      JFrame frame = new JFrame("Swing Tester");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      createUI(frame);
      frame.setSize(560, 200);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   private static void createUI(JFrame frame){
      JPanel panel = new JPanel();
      LayoutManager layout = new FlowLayout();
      panel.setLayout(layout);
      panel.add(new JLabel("Hello World!"));

      frame.getContentPane().add(panel, BorderLayout.CENTER);
   }
}

出力

ウィンドウのルックアンドフィール link:/cgi-bin/printpage.cgi [__ Print]