我想只显示“Swartches”和“RGB”面板。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.colorchooser.AbstractColorChooserPanel;
public class ColorPickerSample {
private static final long serialVersionUID = 1L;
private static String hex = "#ff0033";
private static void createAndShowGUI() {
// Create and set up the window.
final JFrame frame = new JFrame("Centered");
// Display the window.
frame.setSize(50, 100);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set flow layout for the frame
frame.getContentPane().setLayout(new FlowLayout());
JButton button = new JButton("");
System.out.println(Color.decode(hex));
button.setBackground(Color.decode(hex));
button.setPreferredSize(new Dimension(20, 20));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JColorChooser cc = new JColorChooser();
AbstractColorChooserPanel defaultPanels[] = cc.getChooserPanels();
cc.removeChooserPanel(defaultPanels[1]);
cc.removeChooserPanel(defaultPanels[2]);
cc.removeChooserPanel(defaultPanels[4]);
// frame.getContentPane().add(cc);
//Color color = cc.showDialog(frame, "Choose a color", Color.blue);
}
});
frame.getContentPane().add(button);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
如何仅显示2个面板
答案 0 :(得分:3)
API也有removeChooserPanel(...)
方法。
所以我猜你可以这样做:
AbstractColorChooserPanel defaultPanels[] = cc.getChooserPanels();
cc.removeChooserPanel( defaultPanels[4] ); // CMYK
cc.removeChooserPanel( defaultPanels[2] ); // HSL
...
编辑:
我不确定如何在面板
中显示此修改后的选择器
您需要使用JColorChooser的createDialog(...)
方法:
JDialog dialog = JColorChooser.createDialog(
frame.getContentPane(),
"Choose a Color",
true,
cc,
null,
null);
dialog.setVisible(true);
System.out.println( cc.getColor() );