📄 lookandfeeldemo.java
字号:
package components;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;
public class LookAndFeelDemo extends JFrame {
private static final long serialVersionUID = 1L;
private Map<String,String> map;
private JComboBox comboBox;
public LookAndFeelDemo() {
comboBox = new JComboBox();
map = new HashMap<String, String>();
UIManager.LookAndFeelInfo[] feels = UIManager.getInstalledLookAndFeels();
for (LookAndFeelInfo info : feels) {
map.put(info.getName(), info.getClassName());
comboBox.addItem(info.getName());
}
JLabel label = new JLabel("JLabel",SwingConstants.CENTER);
JButton button = new JButton("JButton");
button.setHorizontalAlignment(SwingConstants.CENTER);
JTextField field = new JTextField("JTextField",5);
JTextArea area = new JTextArea(5,5);
JPasswordField psw = new JPasswordField(5);
JRadioButton radio = new JRadioButton("JRadioButton");
JCheckBox check = new JCheckBox("JCheckBox");
Box box = Box.createVerticalBox();
box.add(Box.createVerticalStrut(20));
box.add(comboBox);
box.add(Box.createVerticalStrut(20));
box.add(label);
box.add(Box.createVerticalStrut(20));
box.add(button);
box.add(Box.createVerticalStrut(20));
box.add(field);
box.add(Box.createVerticalStrut(20));
box.add(area);
box.add(Box.createVerticalStrut(20));
box.add(psw);
box.add(Box.createVerticalStrut(20));
box.add(radio);
box.add(Box.createVerticalStrut(20));
box.add(check);
box.add(Box.createVerticalStrut(20));
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String name = (String)comboBox.getSelectedItem();
String className = map.get(name);
try {
UIManager.setLookAndFeel(className);//UnsupportedLookandFeelException
SwingUtilities.updateComponentTreeUI(LookAndFeelDemo.this);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(box);
setSize(300,500);
setVisible(true);
}
public static void main(String[] args) {
LookAndFeelDemo application = new LookAndFeelDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -