📄 demo.java
字号:
/* Demo.java -- Shows examples of AWT components Copyright (C) 1998, 1999, 2002, 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.awt;import java.awt.*;import java.awt.List;import java.awt.event.*;import java.net.URL;import java.util.*;class Demo{ public static void main(String args[]) { MainWindow f = new MainWindow(); f.show(); } static interface SubWindow { public void init (); } static class PrettyPanel extends Panel { Insets myInsets; public PrettyPanel () { myInsets = new Insets (10, 10, 10, 10); } public Insets getInsets () { return myInsets; } } static abstract class PrettyFrame extends Frame { public PrettyFrame () { ((BorderLayout) getLayout ()).setHgap (5); ((BorderLayout) getLayout ()).setVgap (5); } public Insets getInsets() { Insets oldInsets = super.getInsets (); return new Insets (oldInsets.top+10, oldInsets.left+10, oldInsets.bottom+10, oldInsets.right+10); } } static abstract class SubFrame extends PrettyFrame implements SubWindow { boolean initted = false; public void setVisible (boolean visible) { if (!initted && visible) init(); super.setVisible (visible); } } static class MainWindow extends PrettyFrame implements ActionListener { Button closeButton; Hashtable windows; Vector buttons; void addSubWindow (String name, SubWindow w) { Button b = new Button (name); b.addActionListener (this); buttons.addElement (b); windows.put (b, w); } MainWindow () { MenuBar mb = new MenuBar (); Menu menu = new Menu ("File"); Menu submenu = new Menu ("Testing", true); submenu.add (new CheckboxMenuItem ("FooBar")); submenu.add (new CheckboxMenuItem ("BarFoo")); menu.add (submenu); menu.add (new MenuItem("Orange")); MenuItem quit = new MenuItem("Quit", new MenuShortcut('Q')); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(quit); mb.add (menu); menu = new Menu("Edit", true); menu.add(new MenuItem("Cut")); menu.add(new MenuItem("Copy")); menu.add(new MenuItem("Paste")); mb.add (menu); Menu helpMenu = new Menu("Help"); helpMenu.add(new MenuItem("About")); mb.add(helpMenu); mb.setHelpMenu(helpMenu); setMenuBar (mb); String version = System.getProperty("gnu.classpath.version"); add (new Label ("GNU Classpath " + version), "North"); closeButton = new Button ("Close"); closeButton.addActionListener (this); closeButton.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 18)); add (closeButton, "South"); windows = new Hashtable (); buttons = new Vector (); addSubWindow ("Buttons", new ButtonsWindow ()); addSubWindow ("Cursors", new CursorsWindow ()); addSubWindow ("Dialog", new DialogWindow (this)); addSubWindow ("File", new FileWindow (this)); addSubWindow ("Labels", new LabelWindow ()); addSubWindow ("List", new ListWindow ()); addSubWindow ("Radio Buttons", new RadioWindow ()); addSubWindow ("TextField", new TextFieldWindow ()); addSubWindow ("RandomTests", new TestWindow (this)); addSubWindow ("RoundRect", new RoundRectWindow ()); Panel sp = new Panel(); PrettyPanel p = new PrettyPanel(); p.setLayout (new GridLayout (windows.size(), 1)); for (Enumeration e = buttons.elements (); e.hasMoreElements (); ) { p.add ((Button) e.nextElement ()); } sp.add (p); add (sp, "Center"); setTitle ("AWT Demo"); pack(); } public void actionPerformed (ActionEvent evt) { Button source = (Button) evt.getSource (); if (source==closeButton) { dispose(); System.exit (0); } Window w = (Window) windows.get (source); if (w.isVisible ()) w.dispose (); else { if (w instanceof Dialog) { w.show(); } else { w.setVisible (true); } } } } static class ButtonsWindow extends SubFrame implements ActionListener { Button b[] = new Button [9]; public void init () { initted = true; Panel p = new Panel (); p.setLayout (new GridLayout (0, 3, 5, 5)); for (int i=0; i<9; i++) { b[i]=new Button ("button" + (i+1)); b[i].addActionListener (this); } p.add (b[0]); p.add (b[6]); p.add (b[4]); p.add (b[8]); p.add (b[1]); p.add (b[7]); p.add (b[3]); p.add (b[5]); p.add (b[2]); add (p, "North"); Button cb = new Button ("close"); cb.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { dispose(); } }); add (cb, "South"); setTitle ("Buttons"); pack(); } public void actionPerformed (ActionEvent evt) { Button source = (Button) evt.getSource (); for (int i = 0; i < 9; i++) { if (source == b[i]) { int i2 = ((i + 1) == 9) ? 0 : (i + 1); if (b[i2].isVisible()) b[i2].setVisible(false); else b[i2].setVisible(true); } } } } static class DialogWindow extends Dialog implements SubWindow { Label text; Frame parent; boolean initted = false; public DialogWindow (Frame f) { super (f, true); this.parent = f; addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent e) { text.setVisible (false); hide (); } }); } public void setVisible (boolean visible) { if (!initted && visible) init(); super.setVisible (visible); } public void show () { if (!initted) init(); super.show (); } public void init () { text = new Label ("Dialog Test"); text.setAlignment (Label.CENTER); add (text, "North"); text.setVisible (false); Panel p = new PrettyPanel(); Button cb = new Button ("OK"); cb.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { text.setVisible (false); hide(); } }); p.setLayout (new GridLayout (1, 3)); ((GridLayout) p.getLayout ()).setHgap (5); ((GridLayout) p.getLayout ()).setVgap (5); p.add (cb); Button toggle = new Button ("Toggle"); p.add (toggle); toggle.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { if (text.isVisible ()) text.setVisible (false); else text.setVisible (true); doLayout(); } }); Button subdlg = new Button ("SubDialog"); p.add (subdlg); subdlg.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { DialogWindow sw = new DialogWindow (parent); sw.show (); } }); add (p, "South"); setTitle ("Dialog"); pack(); } } static class CursorsWindow extends SubFrame implements ItemListener { Choice cursorChoice; Canvas cursorCanvas; public void init () { cursorChoice = new Choice(); cursorChoice.add ("Default"); cursorChoice.add ("Crosshair"); cursorChoice.add ("Text"); cursorChoice.add ("Wait"); cursorChoice.add ("Southwest Resize"); cursorChoice.add ("Southeast Resize"); cursorChoice.add ("Northwest Resize"); cursorChoice.add ("Northeast Resize"); cursorChoice.add ("North Resize"); cursorChoice.add ("South Resize"); cursorChoice.add ("West Resize"); cursorChoice.add ("East Resize"); cursorChoice.add ("Hand"); cursorChoice.add ("Move"); cursorChoice.addItemListener(this); add (cursorChoice, "North"); cursorCanvas = new Canvas () { public void paint (Graphics g) { Dimension d = this.getSize(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height/2); g.setColor(Color.black); g.fillRect(0, d.height/2, d.width, d.height/2); g.setColor(this.getBackground()); g.fillRect(d.width/3, d.height/3, d.width/3, d.height/3); } }; cursorCanvas.setSize (80,80); add (cursorCanvas, "Center"); Button cb = new Button ("Close"); cb.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { dispose(); } }); add (cb, "South"); setTitle ("Cursors"); pack(); } public void itemStateChanged (ItemEvent e) { int index = cursorChoice.getSelectedIndex(); cursorCanvas.setCursor(Cursor.getPredefinedCursor(index)); } } static class TextFieldWindow extends SubFrame implements ItemListener { Checkbox editable, visible, sensitive; TextField text; public void init () { initted = true; text = new TextField ("hello world"); add (text, "North"); Panel p = new Panel(); p.setLayout (new GridLayout (3, 1)); ((GridLayout) p.getLayout ()).setHgap (5); ((GridLayout) p.getLayout ()).setVgap (5); editable = new Checkbox("Editable", true); p.add (editable); editable.addItemListener (this); visible = new Checkbox("Visible", true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -