简介
java 切换不同的显示风格
code
import java.awt.*;
import javax.swing.*;
public class ImageTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new PlafFrame();
frame.setTitle("ImageTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
;
}
}
/*
* @Author: your name
* @Date: 2020-10-29 10:15:01
* @LastEditTime: 2020-10-29 10:22:43
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /java/PlafFrame.java
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PlafFrame extends JFrame {
private JPanel buttonPanel;
public PlafFrame() {
buttonPanel = new JPanel();
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo info : infos) {
makeButton(info.getName(), info.getClassName());
}
add(buttonPanel);
pack();
}
private void makeButton(String name, String className) {
// add button to panel
javax.swing.JButton button = new JButton(name);
buttonPanel.add(button);
// set button action
button.addActionListener(event -> {
try {
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(this);
pack();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}