📄 swingset2.java~3~
字号:
createToolTipMenuItem(toolTipMenu, "ToolTipMenu.off_label", "ToolTipMenu.off_mnemonic", "ToolTipMenu.off_accessible_description", new ToolTipAction(this, false)); } // ***** create the multiscreen menu, if we have multiple screens if (!isApplet()) { GraphicsDevice[] screens = GraphicsEnvironment. getLocalGraphicsEnvironment(). getScreenDevices(); if (screens.length > 1) { JMenu multiScreenMenu = (JMenu) menuBar.add(new JMenu( getString("MultiMenu.multi_label"))); multiScreenMenu.setMnemonic(getMnemonic("MultiMenu.multi_mnemonic")); multiScreenMenu.getAccessibleContext().setAccessibleDescription( getString("MultiMenu.multi_accessible_description")); createMultiscreenMenuItem(multiScreenMenu, MultiScreenAction.ALL_SCREENS); for (int i = 0; i < screens.length; i++) { createMultiscreenMenuItem(multiScreenMenu, i); } } } return menuBar; } /** * Create the tool tip submenu */ public JMenuItem createToolTipMenuItem(JMenu menu, String label, String mnemonic, String accessibleDescription, Action action) { JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add( new JRadioButtonMenuItem(getString(label))); toolTipMenuGroup.add(mi); mi.setMnemonic(getMnemonic(mnemonic)); mi.getAccessibleContext().setAccessibleDescription(getString( accessibleDescription)); mi.addActionListener(action); return mi; } /** * Create the theme's audio submenu */ public JMenuItem createAudioMenuItem(JMenu menu, String label, String mnemonic, String accessibleDescription, Action action) { JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label))); audioMenuGroup.add(mi); mi.setMnemonic(getMnemonic(mnemonic)); mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription)); mi.addActionListener(action); return mi; } /** * 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 */ public JMenuItem createThemesMenuItem(JMenu menu, String label, String mnemonic, String accessibleDescription, DefaultMetalTheme theme) { JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label))); themesMenuGroup.add(mi); mi.setMnemonic(getMnemonic(mnemonic)); mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription)); mi.addActionListener(new ChangeThemeAction(this, theme)); return mi; } /** * Creates a JRadioButtonMenuItem for the Look and Feel menu */ public JMenuItem createLafMenuItem(JMenu menu, String label, String mnemonic, String accessibleDescription, String laf) { JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label))); lafMenuGroup.add(mi); mi.setMnemonic(getMnemonic(mnemonic)); mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription)); mi.addActionListener(new ChangeLookAndFeelAction(this, laf)); mi.setEnabled(isAvailableLookAndFeel(laf)); return mi; } /** * Creates a multi-screen menu item */ public JMenuItem createMultiscreenMenuItem(JMenu menu, int screen) { JMenuItem mi = null; if (screen == MultiScreenAction.ALL_SCREENS) { mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.all_label"))); mi.setMnemonic(getMnemonic("MultiMenu.all_mnemonic")); mi.getAccessibleContext().setAccessibleDescription(getString( "MultiMenu.all_accessible_description")); } else { mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.single_label") + " " + screen)); mi.setMnemonic(KeyEvent.VK_0 + screen); mi.getAccessibleContext().setAccessibleDescription(getString( "MultiMenu.single_accessible_description") + " " + screen); } mi.addActionListener(new MultiScreenAction(this, screen)); return mi; } public JPopupMenu createPopupMenu() { JPopupMenu popup = new JPopupMenu("JPopupMenu demo"); createPopupMenuItem(popup, "LafMenu.java_label", "LafMenu.java_mnemonic", "LafMenu.java_accessible_description", metal); createPopupMenuItem(popup, "LafMenu.mac_label", "LafMenu.mac_mnemonic", "LafMenu.mac_accessible_description", mac); createPopupMenuItem(popup, "LafMenu.motif_label", "LafMenu.motif_mnemonic", "LafMenu.motif_accessible_description", motif); createPopupMenuItem(popup, "LafMenu.windows_label", "LafMenu.windows_mnemonic", "LafMenu.windows_accessible_description", windows); createPopupMenuItem(popup, "LafMenu.gtk_label", "LafMenu.gtk_mnemonic", "LafMenu.gtk_accessible_description", gtk); // register key binding to activate popup menu InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); map.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.CTRL_MASK), "postMenuAction"); getActionMap().put("postMenuAction", new ActivatePopupMenuAction(this, popup)); return popup; } /** * Creates a JMenuItem for the Look and Feel popup menu */ public JMenuItem createPopupMenuItem(JPopupMenu menu, String label, String mnemonic, String accessibleDescription, String laf) { JMenuItem mi = menu.add(new JMenuItem(getString(label))); popupMenuGroup.add(mi); mi.setMnemonic(getMnemonic(mnemonic)); mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription)); mi.addActionListener(new ChangeLookAndFeelAction(this, laf)); mi.setEnabled(isAvailableLookAndFeel(laf)); return mi; } /** * Load the first demo. This is done separately from the remaining demos * so that we can get SwingSet2 up and available to the user quickly. */ public void preloadFirstDemo() { DemoModule demo = addDemo(new InternalFrameDemo(this)); setDemo(demo); } /** * Add a demo to the toolbar */ public DemoModule addDemo(DemoModule demo) { demosVector.addElement(demo); // do the following on the gui thread SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) { public void run() { SwitchToDemoAction action = new SwitchToDemoAction(swingset, (DemoModule) obj); JToggleButton tb = swingset.getToolBar().addToggleButton(action); swingset.getToolBarGroup().add(tb); if(swingset.getToolBarGroup().getSelection() == null) { tb.setSelected(true); } tb.setText(null); tb.setToolTipText(((DemoModule)obj).getToolTip()); if(demos[demos.length-1].equals(obj.getClass().getName())) { setStatus(getString("Status.popupMenuAccessible")); } } }); return demo; } /** * Sets the current demo */ public void setDemo(DemoModule demo) { 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); } } /** * Show the spash screen while the rest of the demo loads */ public void createSplashScreen() { splashLabel = new JLabel(createImageIcon("Splash.jpg", "Splash.accessible_description")); if(!isApplet()) { splashScreen = new JWindow(getFrame()); splashScreen.getContentPane().add(splashLabel); splashScreen.pack(); Rectangle screenRect = getFrame().getGraphicsConfiguration().getBounds(); splashScreen.setLocation( screenRect.x + screenRect.width/2 - splashScreen.getSize().width/2, screenRect.y + screenRect.height/2 - splashScreen.getSize().height/2); } } public void showSplashScreen() { if(!isApplet()) { splashScreen.show(); } else { add(splashLabel, BorderLayout.CENTER); validate(); repaint(); } } /** * pop down the spash screen */ public void hideSplash() { if(!isApplet()) { splashScreen.setVisible(false); splashScreen = null; splashLabel = null; } } // ******************************************************* // ****************** 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. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -