📄 swingtestlookfeel.java
字号:
package soe;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class SwingTestLookFeel extends JFrame {/*** Change look and feel dinamically*/private static final long serialVersionUID = 1L;JButton btnHello;JButton btnLookFeel;JLabel label;public SwingTestLookFeel() {super("SwingEventTest");setSize(500, 200);getContentPane().setLayout(new GridBagLayout());GridBagConstraints c = new GridBagConstraints();c.fill = GridBagConstraints.HORIZONTAL;// Create button to display hellobtnHello = new JButton("name of L&F is ");getContentPane().add(btnHello);// Register action listener using anonymous classbtnHello.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent evt) {label.setText(UIManager.getLookAndFeel().toString());}});// Change look and feel to motifbtnLookFeel = new JButton("Change to motif");getContentPane().add(btnLookFeel);// Register action listener using anonymous classbtnLookFeel.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent evt) {changeLookFeel();}});// Create labellabel = new JLabel("");c.gridwidth = 2;c.gridx = 0;c.gridy = 1;label.setHorizontalAlignment(JLabel.CENTER);getContentPane().add(label, c);// Window Close eventaddWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});setVisible(true);}// change the look and feel to Motifpublic void changeLookFeel() {try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } SwingUtilities.updateComponentTreeUI(this); } public static void main(String[] args) { new SwingTestLookFeel(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -