📄 demo.java
字号:
/* SwingDemo.java -- An example of using the javax.swing UI. Copyright (C) 2003, 2004 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.border.*;import javax.swing.plaf.metal.DefaultMetalTheme;import javax.swing.plaf.metal.MetalLookAndFeel;import javax.swing.plaf.metal.OceanTheme;import java.net.URL;public class Demo{ JFrame frame; static Color blueGray = new Color(0xdc, 0xda, 0xd5); static { try { if (System.getProperty("swing.defaultlaf") == null) { StringBuffer text = new StringBuffer(); text.append("You may change the Look and Feel of this\n"); text.append("Demo by setting the system property\n"); text.append("-Dswing.defaultlaf=<LAFClassName>\n"); text.append("\n"); text.append("Possible values for <LAFClassName> are:\n"); text.append("\n"); text.append("* javax.swing.plaf.metal.MetalLookAndFeel\n"); text.append(" the default GNU Classpath L&F\n"); text.append("\n"); text.append("* gnu.classpath.examples.swing.GNULookAndFeel\n"); text.append(" the GNU Look and Feel\n"); text.append(" (derived from javax.swing.plaf.basic.BasicLookAndFeel)\n"); text.append("\n"); text.append("MetalLookAndFeel supports different Themes.\n"); text.append("DefaultMetalTheme (the default) and OceanTheme (in development)\n"); final String DEFAULT = "MetalLookAndFeel (default)"; final String OCEAN = "MetalLookAndFeel (Ocean)"; final String GNU = "GNULookAndFeel"; final String[] lafs = new String[] { DEFAULT, OCEAN, GNU }; int laf = JOptionPane.showOptionDialog(null, text /* textPane */, "Look and Feel choice", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, lafs, DEFAULT); if (laf == 0) { MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme()); UIManager.setLookAndFeel(new MetalLookAndFeel()); } if (laf == 1) { MetalLookAndFeel.setCurrentTheme(new OceanTheme()); UIManager.setLookAndFeel(new MetalLookAndFeel()); } else if (laf == 2) UIManager.setLookAndFeel(new GNULookAndFeel()); } } catch (UnsupportedLookAndFeelException e) { System.err.println("Cannot install GNULookAndFeel, exiting"); System.exit(1); } } 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); } 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); } static 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"); new PopUpAction("Buttons", (new ButtonDemo("Button Demo")).createContent(), examples); new PopUpAction("Toggles", mkToggle("cool and refreshing"), examples); new PopUpAction("Checkbox", mkCheckbox("ice cold"), examples); new PopUpAction("Radio", mkRadio("delicious"), examples); new PopUpAction("Slider", (new SliderDemo("Slider Demo")).createContent(), examples); new PopUpAction("ProgressBar", ProgressBarDemo.createContent(), examples); new PopUpAction("List", mkListPanel(new String[] { "hello", "this", "is", "a", "list", "that", "wraps", "over"}), examples); new PopUpAction("Scrollbar", (new ScrollBarDemo("ScrollBarDemo")).createContent(), examples); new PopUpAction("Viewport", mkViewportBox(mkBigButton("View Me!")), examples); new PopUpAction("ScrollPane", mkScrollPane(mkBigButton("Scroll Me!")), examples); new PopUpAction("TabPane", mkTabs(new String[] {"happy", "sad", "indifferent"}), examples); new PopUpAction("Spinner", mkSpinner(), examples); new PopUpAction("TextField", (new TextFieldDemo("TextField Demo")).createContent(), examples); new PopUpAction("FileChooser", (new FileChooserDemo("FileChooser Demo")).createContent(), examples); new PopUpAction("ColorChooser", mkColorChooser(), examples); new PopUpAction("ComboBox", (new ComboBoxDemo("ComboBox Demo")).createContent(), examples); new PopUpAction("Editor", mkEditorPane(), examples); new PopUpAction("Tree", mkTree(), examples); new PopUpAction("Table", mkTable(), examples); 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")); bar.add(file); bar.add(edit); bar.add(examples); bar.add(help); return bar; } 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; } 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"; } } 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); } static JPanel mkButtonWorld() { Icon ii = bigStockIcon("home"); int CENTER = SwingConstants.CENTER; int TOP = SwingConstants.TOP; int BOTTOM = SwingConstants.BOTTOM; int[] valigns = new int[] {SwingConstants.CENTER, SwingConstants.TOP, SwingConstants.BOTTOM}; int[] haligns = new int[] {SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEFT}; Border[] borders = new Border[] { new SoftBevelBorder(BevelBorder.RAISED), new SoftBevelBorder(BevelBorder.LOWERED), new BevelBorder(BevelBorder.RAISED),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -