方法...
以下代碼使用JColorChooser選擇一種顏色,并在標準輸出上打印一條消息:
import java.awt.Color; /*from ww w . j a v a2s.co m*/ import javax.swing.JColorChooser; public class Main { public static void main(String[] args) { // Display a color chooser dialog Color color = JColorChooser.showDialog(null, "Select a color", null); // Check if user selected a color if (color == null) { System.out.println("we cancelled or closed the color chooser"); } else { System.out.println("we selected color: " + color); } } }
下面的代碼顯示了如何顯示顏色選擇器對話框并使用所選顏色設置按鈕背景。
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /*from w ww . jav a 2s .co m*/ import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; public class Main { public static void main(String args[]) { JFrame f = new JFrame("JColorChooser Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton("Pick to Change Background"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color initialBackground = button.getBackground(); Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground); if (background != null) { button.setBackground(background); } } }; button.addActionListener(actionListener); f.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); } }
下面的代碼顯示了如何聽彩色選擇變化事件。
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; /*from w ww . ja va2 s . co m*/ import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.colorchooser.ColorSelectionModel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("JColorChooser Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel("www.o2fo.com", JLabel.CENTER); label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48)); frame.add(label, BorderLayout.SOUTH); final JColorChooser colorChooser = new JColorChooser(label.getBackground()); ColorSelectionModel model = colorChooser.getSelectionModel(); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { Color newForegroundColor = colorChooser.getColor(); label.setForeground(newForegroundColor); } }; model.addChangeListener(changeListener); frame.add(colorChooser, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }
下面的代碼顯示了如何添加自定義顏色選擇器面板到JColorChooser對話框。
import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; /*w w w .j a v a 2 s . co m*/ import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.colorchooser.AbstractColorChooserPanel; public class Main { public static void main(String[] argv) { JColorChooser chooser = new JColorChooser(); chooser.addChooserPanel(new MyChooserPanel()); } } class MyChooserPanel extends AbstractColorChooserPanel { public void buildChooser() { setLayout(new GridLayout(0, 3)); makeAddButton("Red", Color.red); makeAddButton("Green", Color.green); makeAddButton("Blue", Color.blue); } public void updateChooser() { } public String getDisplayName() { return "MyChooserPanel"; } public Icon getSmallDisplayIcon() { return null; } public Icon getLargeDisplayIcon() { return null; } private void makeAddButton(String name, Color color) { JButton button = new JButton(name); button.setBackground(color); button.setAction(setColorAction); add(button); } Action setColorAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { JButton button = (JButton) evt.getSource(); getColorSelectionModel().setSelectedColor(button.getBackground()); } }; }
更多建議: