📄 swingapplication.java
字号:
package chap13.demo;/* * SwingApplication.java is a 1.4 example that requires * no other files. */import javax.swing.*; import java.awt.*;import java.awt.event.*;public class SwingApplication extends JFrame implements ActionListener { private static String labelPrefix = "Number of button clicks: "; private int numClicks = 0; final JLabel label = new JLabel(labelPrefix + "0 "); //Specify the look and feel to use. Valid values: //null (use the default), "Metal", "System", "Motif", "GTK+" UIManager.LookAndFeelInfo[] feel=UIManager.getInstalledLookAndFeels(); static String lookAndFeel = null; public SwingApplication(String title) throws HeadlessException { super(title); //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Component contents = this.createComponents(); this.getContentPane().add(contents, BorderLayout.CENTER); //Display the window. this.pack(); this.setVisible(true); } private Component createComponents() { JButton button = new JButton("I'm a Swing button!"); button.setMnemonic(KeyEvent.VK_I); button.addActionListener(this); label.setLabelFor(button); /* * An easy way to put space between a top-level container * and its contents is to put the contents in a JPanel * that has an "empty" border. */ JPanel pane = new JPanel(new GridLayout(0, 1)); pane.add(button); pane.add(label); pane.setBorder(BorderFactory.createEmptyBorder( 30, //top 30, //left 10, //bottom 30) //right ); return pane; } public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); lookAndFeel=feel[numClicks%feel.length].getClassName(); System.out.println(lookAndFeel); initLookAndFeel(); } private void initLookAndFeel() { try { UIManager.setLookAndFeel(lookAndFeel); SwingUtilities.updateComponentTreeUI(this); this.pack(); this.setVisible(true); } catch (ClassNotFoundException e) { System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel); System.err.println("Did you include the L&F library in the class path?"); System.err.println("Using the default look and feel."); } catch (UnsupportedLookAndFeelException e) { System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform."); System.err.println("Using the default look and feel."); } catch (Exception e) { System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason."); System.err.println("Using the default look and feel."); e.printStackTrace(); } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { SwingApplication app=new SwingApplication("SwingApplication"); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -