📄 metalthemeeditor.java
字号:
/* MetalThemeEditor.java -- Edit themes using this application Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.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.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.classpath.examples.swing;import gnu.javax.swing.plaf.metal.CustomizableTheme;import java.awt.Color;import java.awt.Component;import java.awt.Graphics;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Writer;import javax.swing.BorderFactory;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.plaf.metal.MetalLookAndFeel;/** * This application serves two purposes: 1. demonstrate the color chooser * component, 2. make creating new Metal themes as easy as possible. * * @author Roman Kennke (kennke@aicas.com) */public class MetalThemeEditor extends JPanel{ /** * An icon to display a chosen color in a button. */ private class ColorIcon implements Icon { /** * The color to be shown on the icon. */ Color color; /** * Creates a new ColorIcon. * * @param c the color to be displayed */ ColorIcon(Color c) { color = c; } /** * Returns the icon height, which is 10. * * @return 10 */ public int getIconHeight() { return 10; } /** * Returns the icon width, which is 30. * * @return 30 */ public int getIconWidth() { return 30; } /** * Paints the icon. * * @param c the component to paint on * @param g the graphics to use * @param x the x location * @param y the y location */ public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(color); g.fillRect(x, y, 30, 10); } } /** * Opens up a color chooser and lets the user select a color for the theme. */ private class ChooseColorAction implements ActionListener { /** * The button that will get updated when a new color is selected. */ private JButton button; /** * Specifies which color of the theme should be updated. See constants in * the MetalThemeEditor class. */ private int colorType; /** * Creates a new ChooseColorAction. The specified button will have its * icon updated to the new color if appropriate. * * @param b the button to update * @param type the color type to update */ ChooseColorAction(JButton b, int type) { button = b; colorType = type; } /** * Opens a color chooser and lets the user select a color. */ public void actionPerformed(ActionEvent event) { Color c = JColorChooser.showDialog(button, "Choose a color", getColor(colorType)); if (c != null) { setColor(colorType, c); button.setIcon(new ColorIcon(c)); } } } /** * Denotes the primary1 color of the theme. */ private static final int PRIMARY1 = 0; /** * Denotes the primary2 color of the theme. */ private static final int PRIMARY2 = 1; /** * Denotes the primary3 color of the theme. */ private static final int PRIMARY3 = 2; /** * Denotes the secondary1 color of the theme. */ private static final int SECONDARY1 = 3; /** * Denotes the secondary2 color of the theme. */ private static final int SECONDARY2 = 4; /** * Denotes the secondary3 color of the theme. */ private static final int SECONDARY3 = 5; /** * The theme that is edited. */ CustomizableTheme theme; /** * Creates a new instance of the MetalThemeEditor. */ MetalThemeEditor() { theme = new CustomizableTheme(); setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11)); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(createConfigurationPanel()); add(Box.createVerticalStrut(17)); add(createButtonPanel()); } /** * Creates the main panel of the MetalThemeEditor. This is the upper * area where the colors can be selected. * * @return the main panel */ private JPanel createConfigurationPanel() { JPanel p = new JPanel(); p.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1; c.weighty = 0; c.fill = GridBagConstraints.HORIZONTAL; Insets labelInsets = new Insets(0, 0, 11, 6); Insets buttonInsets = new Insets(0, 0, 11, 0); // Primary 1 JLabel primary1Label = new JLabel("Primary 1:"); c.gridx = 0; c.gridy = 0; c.insets = labelInsets; p.add(primary1Label, c); Icon p1Icon = new ColorIcon(theme.getPrimary1()); JButton primary1Button = new JButton(p1Icon); primary1Button.addActionListener(new ChooseColorAction(primary1Button, PRIMARY1)); //c.weightx = 0; c.gridx = 1; c.insets = buttonInsets; p.add(primary1Button, c); primary1Label.setLabelFor(primary1Button); // Primary 2 JLabel primary2Label = new JLabel("Primary 2:"); c.gridx = 0; c.gridy = 1; c.insets = labelInsets; p.add(primary2Label, c); Icon p2Icon = new ColorIcon(theme.getPrimary2()); JButton primary2Button = new JButton(p2Icon); primary2Button.addActionListener(new ChooseColorAction(primary2Button, PRIMARY2)); c.gridx = 1; c.insets = buttonInsets; p.add(primary2Button, c); primary2Label.setLabelFor(primary2Button); // Primary 3 JLabel primary3Label = new JLabel("Primary 3:"); c.gridx = 0; c.gridy = 2; c.insets = labelInsets; p.add(primary3Label, c); Icon p3Icon = new ColorIcon(theme.getPrimary3()); JButton primary3Button = new JButton(p3Icon); primary3Button.addActionListener(new ChooseColorAction(primary3Button, PRIMARY3));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -