📄 demo.java
字号:
/* SwingDemo.java -- An example of using the javax.swing UI. Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.This file is part of GNU Classpath examples.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.*/package gnu.classpath.examples.swing;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;import javax.swing.plaf.metal.DefaultMetalTheme;import javax.swing.plaf.metal.MetalLookAndFeel;import javax.swing.plaf.metal.MetalTheme;import javax.swing.plaf.metal.OceanTheme;import java.net.URL;public class Demo{ JFrame frame; /** * The main desktop. This is package private to avoid synthetic accessor * method. */ JDesktopPane desktop; /** * The themes menu. This is implemented as a field so that the L&F switcher * can disable the menu when a non-Metal L&F is selected. */ JMenu themesMenu; static Color blueGray = new Color(0xdc, 0xda, 0xd5); private static Icon stockIcon(String s) { return getIcon("/gnu/classpath/examples/icons/stock-" + s + ".png", s); } static Icon bigStockIcon(String s) { return getIcon("/gnu/classpath/examples/icons/big-" + s + ".png", s); } private static Icon getIcon(String location, String name) { URL url = Demo.class.getResource(location); if (url == null) System.err.println("WARNING " + location + " not found."); return new ImageIcon(url, name); } private JMenuBar mkMenuBar() { JMenuBar bar = new JMenuBar(); JMenu file = new JMenu("File"); JMenu edit = new JMenu("Edit"); JMenu help = new JMenu("Help"); file.setMnemonic(KeyEvent.VK_F); edit.setMnemonic(KeyEvent.VK_E); help.setMnemonic(KeyEvent.VK_H); file.add(new JMenuItem("New", stockIcon("new"))); file.add(new JMenuItem("Open", stockIcon("open"))); JMenu recent = new JMenu("Recent Files..."); recent.add(new JMenuItem("war-and-peace.txt")); recent.add(new JMenuItem("taming-of-shrew.txt")); recent.add(new JMenuItem("sun-also-rises.txt")); file.add(recent); file.add(new JMenuItem("Save", stockIcon("save"))); file.add(new JMenuItem("Save as...", stockIcon("save-as"))); JMenuItem exit = new JMenuItem("Exit", stockIcon("quit")); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(1); } }); file.add(exit); edit.add(new JMenuItem("Cut", stockIcon("cut"))); edit.add(new JMenuItem("Copy", stockIcon("copy"))); edit.add(new JMenuItem("Paste", stockIcon("paste"))); JMenu preferences = new JMenu("Preferences..."); preferences.add(new JCheckBoxMenuItem("Microphone Active", stockIcon("mic"))); preferences.add(new JCheckBoxMenuItem("Check Spelling", stockIcon("spell-check"))); preferences.add(new JCheckBoxMenuItem("World Peace")); preferences.add(new JSeparator()); preferences.add(new JRadioButtonMenuItem("Radio Button")); edit.add(preferences); JMenu examples = new JMenu("Examples"); examples.add(new JMenuItem(new PopupAction("Buttons", ButtonDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("Slider", SliderDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("ProgressBar", ProgressBarDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("Scrollbar", ScrollBarDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("Spinner", SpinnerDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("TextField", TextFieldDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("TextArea", TextAreaDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("FileChooser", FileChooserDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("ComboBox", ComboBoxDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("Table", TableDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("List", ListDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("TabbedPane", TabbedPaneDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("Tree", TreeDemo.createDemoFactory()))); examples.add(new JMenuItem(new PopupAction("Theme Editor", MetalThemeEditor.createDemoFactory()))); final JMenuItem vmMenu; help.add(new JMenuItem("just play with the widgets")); help.add(new JMenuItem("and enjoy the sensation of")); help.add(new JMenuItem("your neural connections growing")); help.add(new JSeparator()); help.add(vmMenu = new JMenuItem("Really, which VM is this running on?")); vmMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String message = "This is " + System.getProperty("java.vm.name") + " Version " + System.getProperty("java.vm.version") + " distributed by " + System.getProperty("java.vm.vendor"); String gnuClasspath = System.getProperty("gnu.classpath.version"); if(gnuClasspath != null) message += "\nThe runtime's libraries are " + "kindly provided by the " + "members of GNU Classpath and are in version " + gnuClasspath + "."; JOptionPane.showMessageDialog(vmMenu, message); } }); // Create L&F menu. JMenu lafMenu = new JMenu("Look and Feel"); ButtonGroup lafGroup = new ButtonGroup(); UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels(); String currentLaf = UIManager.getLookAndFeel().getClass().getName(); for (int i = 0; i < lafs.length; ++i) { UIManager.LookAndFeelInfo laf = lafs[i]; ChangeLAFAction action = new ChangeLAFAction(laf); JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(action); boolean selected = laf.getClassName().equals(currentLaf); lafItem.setSelected(selected); lafMenu.add(lafItem); } // Create themes menu. themesMenu = new JMenu("Themes"); ButtonGroup themesGroup = new ButtonGroup(); JRadioButtonMenuItem ocean = new JRadioButtonMenuItem(new ChangeThemeAction(new OceanTheme())); ocean.setSelected(MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme); themesMenu.add(ocean); themesGroup.add(ocean); JRadioButtonMenuItem steel = new JRadioButtonMenuItem(new ChangeThemeAction(new DefaultMetalTheme())); ocean.setSelected(MetalLookAndFeel.getCurrentTheme() instanceof DefaultMetalTheme); themesMenu.add(steel); themesGroup.add(steel); bar.add(file); bar.add(edit); bar.add(examples); bar.add(lafMenu); bar.add(themesMenu); bar.add(help); return bar; } private static void triggerDialog(final JButton but, final String dir) { but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showConfirmDialog(but, "Sure you want to go " + dir + "?", "Confirm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, bigStockIcon("warning")); } }); } static JToolBar mkToolBar() { JToolBar bar = new JToolBar(); JButton b = mkButton(stockIcon("go-back")); triggerDialog(b, "back"); bar.add(b); b = mkButton(stockIcon("go-down")); triggerDialog(b, "down"); bar.add(b); b = mkButton(stockIcon("go-forward")); triggerDialog(b, "forward"); bar.add(b); return bar; } private static String valign2str(int a) { switch (a) { case SwingConstants.CENTER: return "Center"; case SwingConstants.TOP: return "Top"; case SwingConstants.BOTTOM: return "Bottom"; default: return "Unknown"; } } static String halign2str(int a) { switch (a) { case SwingConstants.CENTER: return "Center"; case SwingConstants.RIGHT: return "Right"; case SwingConstants.LEFT: return "Left"; default: return "Unknown"; } } private static JButton mkButton(String title, Icon icon, int hAlign, int vAlign, int hPos, int vPos) { JButton b; if (icon == null) b = new JButton(title); else if (title == null) b = new JButton(icon); else b = new JButton(title, icon); b.setToolTipText(title); if (hAlign != -1) b.setHorizontalAlignment(hAlign); if (vAlign != -1) b.setVerticalAlignment(vAlign); if (hPos != -1) b.setHorizontalTextPosition(hPos); if (vPos != -1) b.setVerticalTextPosition(vPos); return b; } static JButton mkButton(String title) { return mkButton(title, null, -1, -1, -1, -1); } static JButton mkButton(Icon i) { return mkButton(null, i, -1, -1, -1, -1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -