📄 metalthemeeditor.java
字号:
c.gridx = 1; c.insets = buttonInsets; p.add(primary3Button, c); primary3Label.setLabelFor(primary3Button); // Secondary 1 JLabel secondary1Label = new JLabel("Secondary 1:"); c.gridx = 0; c.gridy = 3; c.insets = labelInsets; p.add(secondary1Label, c); Icon s1Icon = new ColorIcon(theme.getSecondary1()); JButton secondary1Button = new JButton(s1Icon); secondary1Button.addActionListener(new ChooseColorAction(secondary1Button, SECONDARY1)); c.gridx = 1; c.insets = buttonInsets; p.add(secondary1Button, c); secondary1Label.setLabelFor(secondary1Button); // Secondary 2 JLabel secondary2Label = new JLabel("Secondary 2:"); c.gridx = 0; c.gridy = 4; c.insets = labelInsets; p.add(secondary2Label, c); Icon s2Icon = new ColorIcon(theme.getSecondary2()); JButton secondary2Button = new JButton(s2Icon); secondary2Button.addActionListener(new ChooseColorAction(secondary2Button, SECONDARY2)); c.gridx = 1; c.insets = buttonInsets; p.add(secondary2Button, c); secondary2Label.setLabelFor(secondary2Button); // Secondary 3 JLabel secondary3Label = new JLabel("Secondary 3:"); c.gridx = 0; c.gridy = 5; c.insets = labelInsets; p.add(secondary3Label, c); Icon s3Icon = new ColorIcon(theme.getSecondary3()); JButton secondary3Button = new JButton(s3Icon); secondary3Button.addActionListener(new ChooseColorAction(secondary3Button, SECONDARY3)); c.gridx = 1; c.insets = buttonInsets; p.add(secondary3Button, c); secondary3Label.setLabelFor(secondary3Button); return p; } /** * Creates the button panel at the bottom of the MetalThemeEditor. * * @return the button panel */ private JPanel createButtonPanel() { JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); p.add(Box.createHorizontalGlue()); JButton applyButton = new JButton("Apply"); applyButton.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent ev) { try { CustomizableTheme copy = (CustomizableTheme) theme.clone(); MetalLookAndFeel.setCurrentTheme(copy); UIManager.setLookAndFeel(new MetalLookAndFeel()); Window w = SwingUtilities.getWindowAncestor(MetalThemeEditor.this); SwingUtilities.updateComponentTreeUI(w); } catch (Exception ex) { ex.printStackTrace(); } } }); p.add(applyButton); p.add(Box.createHorizontalStrut(5)); JButton exportButton = new JButton("Export as Java File"); exportButton.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent ev) { export(); } }); p.add(exportButton); return p; } /** * Exports the current theme as Java source file. This will prompt the user * to choose a filename. */ void export() { JFileChooser chooser = new JFileChooser(); int confirm = chooser.showSaveDialog(this); if (confirm == JFileChooser.APPROVE_OPTION) exportToFile(chooser.getSelectedFile()); } /** * Writes out the current configured Metal theme as Java source file. * * @param file the file to write into */ void exportToFile(File file) { String fileName = file.getName(); if (! fileName.endsWith(".java")) { JOptionPane.showMessageDialog(this, "Filename does not denote a Java source file", "Invalid filename", JOptionPane.ERROR_MESSAGE); return; } String className = fileName.substring(0, fileName.length() - 5); Color p1 = theme.getPrimary1(); Color p2 = theme.getPrimary2(); Color p3 = theme.getPrimary3(); Color s1 = theme.getSecondary1(); Color s2 = theme.getSecondary2(); Color s3 = theme.getSecondary3(); try { FileOutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out); writer.write("import javax.swing.plaf.ColorUIResource;\n"); writer.write("import javax.swing.plaf.metal.DefaultMetalTheme;\n"); writer.write("public class " + className + " extends DefaultMetalTheme\n"); writer.write("{\n"); writer.write(" protected ColorUIResource getPrimary1()\n"); writer.write(" {\n"); writer.write(" return new ColorUIResource(" + p1.getRGB() + ");\n"); writer.write(" }\n"); writer.write(" protected ColorUIResource getPrimary2()\n"); writer.write(" {\n"); writer.write(" return new ColorUIResource(" + p2.getRGB() + ");\n"); writer.write(" }\n"); writer.write(" protected ColorUIResource getPrimary3()\n"); writer.write(" {\n"); writer.write(" return new ColorUIResource(" + p3.getRGB() + ");\n"); writer.write(" }\n"); writer.write(" protected ColorUIResource getSecondary1()\n"); writer.write(" {\n"); writer.write(" return new ColorUIResource(" + s1.getRGB() + ");\n"); writer.write(" }\n"); writer.write(" protected ColorUIResource getSecondary2()\n"); writer.write(" {\n"); writer.write(" return new ColorUIResource(" + s2.getRGB() + ");\n"); writer.write(" }\n"); writer.write(" protected ColorUIResource getSecondary3()\n"); writer.write(" {\n"); writer.write(" return new ColorUIResource(" + s3.getRGB() + ");\n"); writer.write(" }\n"); writer.write("}\n"); writer.close(); out.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } /** * Returns the color of the theme with the specified type. For the possible * types see the constants of this class. * * @param colorType the color type to fetch from the theme * * @return the current color of the specified type * * @throws IllegalArgumentException for illegal color types */ Color getColor(int colorType) { Color color = null; switch (colorType) { case PRIMARY1: color = theme.getPrimary1(); break; case PRIMARY2: color = theme.getPrimary2(); break; case PRIMARY3: color = theme.getPrimary3(); break; case SECONDARY1: color = theme.getSecondary1(); break; case SECONDARY2: color = theme.getSecondary2(); break; case SECONDARY3: color = theme.getSecondary3(); break; default: throw new IllegalArgumentException("Unknown color type: " + colorType); } return color; } /** * Sets the color of the specified type in the current theme. * * @param colorType the color type * @param color the color to set * * @throws IllegalArgumentException for illegal color types */ void setColor(int colorType, Color color) { switch (colorType) { case PRIMARY1: theme.setPrimary1(color); break; case PRIMARY2: theme.setPrimary2(color); break; case PRIMARY3: theme.setPrimary3(color); break; case SECONDARY1: theme.setSecondary1(color); break; case SECONDARY2: theme.setSecondary2(color); break; case SECONDARY3: theme.setSecondary3(color); break; default: throw new IllegalArgumentException("Illegal color type: " + colorType); } } /** * The entry point to the application. * * @param args ignored */ public static void main(String[] args) { JFrame f = new JFrame("MetalThemeEditor"); f.setContentPane(new MetalThemeEditor()); f.pack(); f.setVisible(true); } /** * Returns a DemoFactory that creates a MetalThemeEditor. * * @return a DemoFactory that creates a MetalThemeEditor */ public static DemoFactory createDemoFactory() { return new DemoFactory() { public JComponent createDemo() { return new MetalThemeEditor(); } }; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -