📄 swingset2.java
字号:
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; } // ******************************************************* // ****************** Utility Methods ******************** // ******************************************************* 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; } } public GumViewer getFrame() { return frame; } /** * Returns the menubar */ public JMenuBar getMenuBar() { return menuBar; } /** * 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; } /** * 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; themesMenu.setEnabled(laf == metal); updateLookAndFeel(); } } /** * Sets the current L&F on each demo module */ public void updateLookAndFeel() { try { UIManager.setLookAndFeel(currentLookAndFeel); SwingUtilities.updateComponentTreeUI(this); } catch (Exception ex) { System.out.println("Failed loading L&F: " + currentLookAndFeel); System.out.println(ex); }
} // ******************************************************* // ******************** Actions *********************** // ******************************************************* class OkAction extends AbstractAction { JDialog aboutBox; protected OkAction(JDialog aboutBox) { super("OkAction"); this.aboutBox = aboutBox; } public void actionPerformed(ActionEvent e) { aboutBox.setVisible(false); } } class ChangeLookAndFeelAction extends AbstractAction { SwingSet2 swingset; String laf; protected ChangeLookAndFeelAction(SwingSet2 swingset, String laf) { super("ChangeTheme"); this.swingset = swingset; this.laf = laf; } public void actionPerformed(ActionEvent e) { swingset.setLookAndFeel(laf); if(swingset.getFrame().isDragged)
swingset.getFrame().col_default=swingset.getFrame().btn_selected.getBackground();
else
swingset.getFrame().col_default=swingset.getFrame().btn_drag.getBackground(); } } class ChangeThemeAction extends AbstractAction { SwingSet2 swingset; DefaultMetalTheme theme; protected ChangeThemeAction(SwingSet2 swingset, DefaultMetalTheme theme) { super("ChangeTheme"); this.swingset = swingset; this.theme = theme;
} public void actionPerformed(ActionEvent e) { MetalLookAndFeel.setCurrentTheme(theme); swingset.updateLookAndFeel(); if(swingset.getFrame().isDragged)
swingset.getFrame().col_default=swingset.getFrame().btn_selected.getBackground();
else
swingset.getFrame().col_default=swingset.getFrame().btn_drag.getBackground();
} } class ExitAction extends AbstractAction { SwingSet2 swingset; protected ExitAction(SwingSet2 swingset) { super("ExitAction"); this.swingset = swingset; } public void actionPerformed(ActionEvent e) { System.exit(0); } } class AboutAction extends AbstractAction { SwingSet2 swingset; protected AboutAction(SwingSet2 swingset) { super("AboutAction"); this.swingset = swingset; } public void actionPerformed(ActionEvent e) { swingset.showAbout(); }}
class FileOpenAction extends AbstractAction { SwingSet2 swingset; protected FileOpenAction(SwingSet2 swingset) { super("FileOpenAction"); this.swingset = swingset; }
public void actionPerformed(ActionEvent e) { swingset.getFrame().openFile(); }}
class AboutPanel extends JPanel { ImageIcon aboutimage = null; SwingSet2 swingset = null; public AboutPanel(SwingSet2 swingset) { this.swingset = swingset; aboutimage = swingset.createImageIcon("About.jpg", "AboutBox.accessible_description"); setOpaque(false); } public void paint(Graphics g) { aboutimage.paintIcon(this, g, 0, 0); super.paint(g); } public Dimension getPreferredSize() { return new Dimension(aboutimage.getIconWidth(), aboutimage.getIconHeight()); } }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -