下面的範例展示了如何將視窗的外觀設定為基於Swing的應用程式中的當前系統。
使用以下API -
UIManager.getSystemLookAndFeelClassName()
- 獲取當前系統的外觀。UIManager.setLookAndFeel()
- 設定UI元件的外觀。JFrame.setDefaultLookAndFeelDecorated(true);
- 改變框架的外觀和感覺。範例
package com.yiibai.swingdemo;
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.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
JFrame.setDefaultLookAndFeelDecorated(true);
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("將Swing視窗的外觀設定為當前系統");
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("Welcome to tw511.com!"));
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
執行上面範例程式碼,得到以下結果: