package org.nhooo.example.swing;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
public class LookAndFeelDemo extends JFrame {
public LookAndFeelDemo() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new LookAndFeelDemo().setVisible(true));
}
private void initComponents() {
setSize(250, 250);
setTitle("LAF Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
JMenu menu = new JMenu("Look and Feel");
// 获取我们将用于的所有可用外观
// 创建JMenuItem并分配操作侦听器来处理
// 选择菜单项以更改外观。
UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo lookAndFeelInfo : lookAndFeels) {
JMenuItem item = new JMenuItem(lookAndFeelInfo.getName());
item.addActionListener(event -> {
try {
// 设置框架的外观并更新UI
// 使用新选择的外观。
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
});
menu.add(item);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
getContentPane().add(menuBar);
getContentPane().add(new JButton("Hello"));
}
}
外观演示