📄 swingset2.java
字号:
/* * @(#)SwingSet2.java 1.19 99/11/08 * * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */import javax.swing.*;import javax.swing.event.*;import javax.swing.text.*;import javax.swing.border.*;import javax.swing.colorchooser.*;import javax.swing.filechooser.*;import javax.accessibility.*;import javax.swing.plaf.metal.DefaultMetalTheme;import javax.swing.plaf.metal.MetalLookAndFeel;import java.lang.reflect.*;import java.awt.*;import java.awt.event.*;import java.beans.*;import java.util.*;import java.io.*;
/** * A demo that shows all of the Swing components. * * @version 1.19 11/08/99 * @author Jeff Dinkins */public class SwingSet2 extends JPanel { // Possible Look & Feels private String mac = "com.sun.java.swing.plaf.mac.MacLookAndFeel"; private String metal = "javax.swing.plaf.metal.MetalLookAndFeel"; private String motif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; private String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; // The current Look & Feel private String currentLookAndFeel = metal; // The preferred size of the demo private int PREFERRED_WIDTH = 680; private int PREFERRED_HEIGHT = 640; // Box spacers private Dimension HGAP = new Dimension(1,5); private Dimension VGAP = new Dimension(5,1); // Resource bundle for internationalized and accessible text private ResourceBundle bundle = null; // About Box private JDialog aboutBox = null; // Menus private JMenuBar menuBar = null; private JMenu themesMenu = null; private ButtonGroup lafMenuGroup = new ButtonGroup(); private ButtonGroup themesMenuGroup = new ButtonGroup(); // Used only if swingset is an application private static GumViewer frame = null; // To debug or not to debug, that is the question private boolean DEBUG = true; private int debugCounter = 0; /** * SwingSet2 Constructor */ public SwingSet2(GumViewer in) { // Note that the applet may null if this is started as an application frame=in; // setLayout(new BorderLayout()); setLayout(new BorderLayout()); // set the preferred size of the demo setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT)); menuBar = createMenus(); add(menuBar, BorderLayout.NORTH); } public void showAbout()
{ if(aboutBox == null) { // JPanel panel = new JPanel(new BorderLayout()); JPanel panel = new AboutPanel(this); panel.setLayout(new BorderLayout()); aboutBox = new JDialog(getFrame(), getString("AboutBox.title"), false); aboutBox.getContentPane().add(panel, BorderLayout.CENTER); // JButton button = new JButton(getString("AboutBox.ok_button_text")); JPanel buttonpanel = new JPanel(); buttonpanel.setOpaque(false); JButton button = (JButton) buttonpanel.add( new JButton(getString("AboutBox.ok_button_text")) ); panel.add(buttonpanel, BorderLayout.SOUTH); button.addActionListener(new OkAction(aboutBox)); } aboutBox.pack(); Point p = getLocationOnScreen(); aboutBox.setLocation(p.x + 10, p.y +10); aboutBox.show(); } /** * Create menus */ public JMenuBar createMenus() { JMenuItem mi; // ***** create the menubar **** JMenuBar menuBar = new JMenuBar(); menuBar.getAccessibleContext().setAccessibleName( getString("MenuBar.accessible_description")); // ***** create File menu JMenu fileMenu = (JMenu) menuBar.add(new JMenu(getString("FileMenu.file_label"))); fileMenu.setMnemonic(getMnemonic("FileMenu.file_mnemonic")); fileMenu.getAccessibleContext().setAccessibleDescription(getString("FileMenu.accessible_description")); createMenuItem(fileMenu, "FileMenu.about_label", "FileMenu.about_mnemonic", "FileMenu.about_accessible_description", new AboutAction(this)); fileMenu.addSeparator(); createMenuItem(fileMenu, "FileMenu.open_label", "FileMenu.open_mnemonic", "FileMenu.open_accessible_description", new FileOpenAction(this)); createMenuItem(fileMenu, "FileMenu.save_label", "FileMenu.save_mnemonic", "FileMenu.save_accessible_description", null); createMenuItem(fileMenu, "FileMenu.save_as_label", "FileMenu.save_as_mnemonic", "FileMenu.save_as_accessible_description", null); fileMenu.addSeparator(); createMenuItem(fileMenu, "FileMenu.exit_label", "FileMenu.exit_mnemonic", "FileMenu.exit_accessible_description", new ExitAction(this) ); // ***** create laf switcher menu JMenu lafMenu = (JMenu) menuBar.add(new JMenu(getString("LafMenu.laf_label"))); lafMenu.setMnemonic(getMnemonic("LafMenu.laf_mnemonic")); lafMenu.getAccessibleContext().setAccessibleDescription( getString("LafMenu.laf_accessible_description")); mi = createLafMenuItem(lafMenu, "LafMenu.java_label", "LafMenu.java_mnemonic", "LafMenu.java_accessible_description", metal); mi.setSelected(true); // this is the default l&f createLafMenuItem(lafMenu, "LafMenu.mac_label", "LafMenu.mac_mnemonic", "LafMenu.mac_accessible_description", mac); createLafMenuItem(lafMenu, "LafMenu.motif_label", "LafMenu.motif_mnemonic", "LafMenu.motif_accessible_description", motif); createLafMenuItem(lafMenu, "LafMenu.windows_label", "LafMenu.windows_mnemonic", "LafMenu.windows_accessible_description", windows); // ***** create themes menu themesMenu = (JMenu) menuBar.add(new JMenu(getString("ThemesMenu.themes_label"))); themesMenu.setMnemonic(getMnemonic("ThemesMenu.themes_mnemonic")); themesMenu.getAccessibleContext().setAccessibleDescription( getString("ThemesMenu.themes_accessible_description")); mi = createThemesMenuItem(themesMenu, "ThemesMenu.default_label", "ThemesMenu.default_mnemonic", "ThemesMenu.default_accessible_description", new DefaultMetalTheme()); mi.setSelected(true); // This is the default theme createThemesMenuItem(themesMenu, "ThemesMenu.aqua_label", "ThemesMenu.aqua_mnemonic", "ThemesMenu.aqua_accessible_description", new AquaTheme()); createThemesMenuItem(themesMenu, "ThemesMenu.charcoal_label", "ThemesMenu.charcoal_mnemonic", "ThemesMenu.charcoal_accessible_description", new CharcoalTheme()); createThemesMenuItem(themesMenu, "ThemesMenu.contrast_label", "ThemesMenu.contrast_mnemonic", "ThemesMenu.contrast_accessible_description", new ContrastTheme()); createThemesMenuItem(themesMenu, "ThemesMenu.emerald_label", "ThemesMenu.emerald_mnemonic", "ThemesMenu.emerald_accessible_description", new EmeraldTheme()); createThemesMenuItem(themesMenu, "ThemesMenu.ruby_label", "ThemesMenu.ruby_mnemonic", "ThemesMenu.ruby_accessible_description", new RubyTheme()); return menuBar; } /** * Creates a generic menu item */ public JMenuItem createMenuItem(JMenu menu, String label, String mnemonic, String accessibleDescription, Action action) { JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(getString(label))); mi.setMnemonic(getMnemonic(mnemonic)); mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription)); mi.addActionListener(action); if(action == null) { mi.setEnabled(false); } return mi; } /** * Creates a JRadioButtonMenuItem for the Themes menu */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -