swingset2.java

来自「一个小公司要求给写的很简单的任务管理系统。」· Java 代码 · 共 1,408 行 · 第 1/4 页

JAVA
1,408
字号
	currentDemo = demo;	// Ensure panel's UI is current before making visible	JComponent currentDemoPanel = demo.getDemoPanel();	SwingUtilities.updateComponentTreeUI(currentDemoPanel);	demoPanel.removeAll();	demoPanel.add(currentDemoPanel, BorderLayout.CENTER);	tabbedPane.setSelectedIndex(0);	tabbedPane.setTitleAt(0, demo.getName());	tabbedPane.setToolTipTextAt(0, demo.getToolTip());    }    /**     * Bring up the SwingSet2 demo by showing the frame (only     * applicable if coming up as an application, not an applet);     */    public void showSwingSet2() {	if(!isApplet() && getFrame() != null) {	    // put swingset in a frame and show it	    JFrame f = getFrame();	    f.setTitle(getString("Frame.title"));	    f.getContentPane().add(this, BorderLayout.CENTER);	    f.pack();	    Rectangle screenRect = f.getGraphicsConfiguration().getBounds();            Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(                    f.getGraphicsConfiguration());            // Make sure we don't place the demo off the screen.            int centerWidth = screenRect.width < f.getSize().width ?                    screenRect.x :                    screenRect.x + screenRect.width/2 - f.getSize().width/2;            int centerHeight = screenRect.height < f.getSize().height ?                    screenRect.y :                    screenRect.y + screenRect.height/2 - f.getSize().height/2;            centerHeight = centerHeight < screenInsets.top ?                    screenInsets.top : centerHeight;            f.setLocation(centerWidth, centerHeight);	    f.show();            numSSs++;            swingSets.add(this);	}     }    // *******************************************************    // ****************** Utility Methods ********************    // *******************************************************    /**     * Loads a demo from a classname     */    void loadDemo(String classname) {	setStatus(getString("Status.loading") + getString(classname + ".name"));	DemoModule demo = null;	try {	    Class demoClass = Class.forName(classname);	    Constructor demoConstructor = demoClass.getConstructor(new Class[]{SwingSet2.class});	    demo = (DemoModule) demoConstructor.newInstance(new Object[]{this});	    addDemo(demo);	} catch (Exception e) {	    System.out.println("Error occurred loading demo: " + classname);	}    }        /**     * A utility function that layers on top of the LookAndFeel's     * isSupportedLookAndFeel() method. Returns true if the LookAndFeel     * is supported. Returns false if the LookAndFeel is not supported     * and/or if there is any kind of error checking if the LookAndFeel     * is supported.     *     * The L&F menu will use this method to detemine whether the various     * L&F options should be active or inactive.     *     */     protected boolean isAvailableLookAndFeel(String laf) {         try {              Class lnfClass = Class.forName(laf);             LookAndFeel newLAF = (LookAndFeel)(lnfClass.newInstance());             return newLAF.isSupportedLookAndFeel();         } catch(Exception e) { // If ANYTHING weird happens, return false             return false;         }     }    /**     * Determines if this is an applet or application     */    public boolean isApplet() {	return (applet != null);    }    /**     * Returns the applet instance     */    public SwingSet2Applet getApplet() {	return applet;    }    /**     * Returns the frame instance     */    public JFrame getFrame() {	return frame;    }    /**     * Returns the menubar     */    public JMenuBar getMenuBar() {	return menuBar;    }    /**     * Returns the toolbar     */    public ToggleButtonToolBar getToolBar() {	return toolbar;    }    /**     * Returns the toolbar button group     */    public ButtonGroup getToolBarGroup() {	return toolbarGroup;    }    /**     * Returns the content pane wether we're in an applet     * or application     */    public Container getContentPane() {	if(contentPane == null) {	    if(getFrame() != null) {		contentPane = getFrame().getContentPane();	    } else if (getApplet() != null) {		contentPane = getApplet().getContentPane();	    }	}	return contentPane;    }    /**     * Create a frame for SwingSet2 to reside in if brought up     * as an application.     */    public static JFrame createFrame(GraphicsConfiguration gc) {	JFrame frame = new JFrame(gc);        if (numSSs == 0) {            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        } else {	    WindowListener l = new WindowAdapter() {	        public void windowClosing(WindowEvent e) {                    numSSs--;                    swingSets.remove(this);	        }	    };	    frame.addWindowListener(l);        }	return frame;    }    /**     * Set the status      */    public void setStatus(String s) {	// do the following on the gui thread	SwingUtilities.invokeLater(new SwingSetRunnable(this, s) {	    public void run() {		swingset.statusField.setText((String) obj);	    }	});    }    /**     * This method returns a string from the demo's resource bundle.     */    public String getString(String key) {	String value = null;	try {	    value = getResourceBundle().getString(key);	} catch (MissingResourceException e) {	    System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);	}	if(value == null) {	    value = "Could not find resource: " + key + "  ";	}	return value;    }    void setDragEnabled(boolean dragEnabled) {        if (dragEnabled == this.dragEnabled) {            return;        }        this.dragEnabled = dragEnabled;        for (DemoModule dm : demosList) {            dm.updateDragEnabled(dragEnabled);        }        demoSrcPane.setDragEnabled(dragEnabled);    }        boolean isDragEnabled() {        return dragEnabled;    }    /**     * Returns the resource bundle associated with this demo. Used     * to get accessable and internationalized strings.     */    public ResourceBundle getResourceBundle() {	if(bundle == null) {	    bundle = ResourceBundle.getBundle("resources.swingset");	}	return bundle;    }    /**     * Returns a mnemonic from the resource bundle. Typically used as     * keyboard shortcuts in menu items.     */    public char getMnemonic(String key) {	return (getString(key)).charAt(0);    }    /**     * Creates an icon from an image contained in the "images" directory.     */    public ImageIcon createImageIcon(String filename, String description) {	String path = "/resources/images/" + filename;	return new ImageIcon(getClass().getResource(path));     }    /**     * If DEBUG is defined, prints debug information out to std ouput.     */    public void debug(String s) {	if(DEBUG) {	    System.out.println((debugCounter++) + ": " + s);	}    }    /**     * Stores the current L&F, and calls updateLookAndFeel, below     */    public void setLookAndFeel(String laf) {	if(currentLookAndFeel != laf) {	    currentLookAndFeel = laf;            /* The recommended way of synchronizing state between multiple             * controls that represent the same command is to use Actions.             * The code below is a workaround and will be replaced in future             * version of SwingSet2 demo.             */            String lafName = null;            if(laf == mac) lafName = getString("LafMenu.mac_label");            if(laf == metal) lafName = getString("LafMenu.java_label");            if(laf == motif) lafName = getString("LafMenu.motif_label");            if(laf == windows) lafName = getString("LafMenu.windows_label");            if(laf == gtk) lafName = getString("LafMenu.gtk_label");	    themesMenu.setEnabled(laf == metal);	    updateLookAndFeel();            for(int i=0;i<lafMenu.getItemCount();i++) {                JMenuItem item = lafMenu.getItem(i);                if(item.getText() == lafName) {                    item.setSelected(true);                } else {                    item.setSelected(false);                }            }	}    }    private void updateThisSwingSet() {        if (isApplet()) {            SwingUtilities.updateComponentTreeUI(getApplet());        } else {            JFrame frame = getFrame();            if (frame == null) {                SwingUtilities.updateComponentTreeUI(this);            } else {                SwingUtilities.updateComponentTreeUI(frame);            }        }        SwingUtilities.updateComponentTreeUI(popupMenu);        if (aboutBox != null) {            SwingUtilities.updateComponentTreeUI(aboutBox);        }    }    /**     * Sets the current L&F on each demo module     */    public void updateLookAndFeel() {	try {	    UIManager.setLookAndFeel(currentLookAndFeel);            if (isApplet()) {                updateThisSwingSet();            } else {                for (SwingSet2 ss : swingSets) {                    ss.updateThisSwingSet();                }            }	} catch (Exception ex) {	    System.out.println("Failed loading L&F: " + currentLookAndFeel);	    System.out.println(ex);	}    }    /**     * Loads and puts the source code text into JEditorPane in the "Source Code" tab     */    public void setSourceCode(DemoModule demo) {	// do the following on the gui thread	SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {	    public void run() {		swingset.demoSrcPane.setText(((DemoModule)obj).getSourceCode());		swingset.demoSrcPane.setCaretPosition(0);	    }	});    }    // *******************************************************    // **************   ToggleButtonToolbar  *****************    // *******************************************************    static Insets zeroInsets = new Insets(1,1,1,1);    protected class ToggleButtonToolBar extends JToolBar {	public ToggleButtonToolBar() {	    super();	}	JToggleButton addToggleButton(Action a) {	    JToggleButton tb = new JToggleButton(		(String)a.getValue(Action.NAME),		(Icon)a.getValue(Action.SMALL_ICON)	    );	    tb.setMargin(zeroInsets);	    tb.setText(null);	    tb.setEnabled(a.isEnabled());	    tb.setToolTipText((String)a.getValue(Action.SHORT_DESCRIPTION));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?