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);    
   }
}

输出

look and feel of a Window
swingexamples_frames.htm
广告