gpt4 book ai didi

java - 在 JColorChooser 中仅显示 2 个面板

转载 作者:行者123 更新时间:2023-12-01 21:54:29 30 4
gpt4 key购买 nike

我只想显示“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 个面板

最佳答案

该 API 还有一个 removeChooserPanel(...) 方法。

所以我想你可以这样做:

AbstractColorChooserPanel defaultPanels[] = cc.getChooserPanels();
cc.removeChooserPanel( defaultPanels[4] ); // CMYK
cc.removeChooserPanel( defaultPanels[2] ); // HSL
...

编辑:

I am not sure how would I display this modified chooser in panel

您将需要使用 JColorChooser 的 createDialog(...) 方法:

JDialog dialog = JColorChooser.createDialog(
frame.getContentPane(),
"Choose a Color",
true,
cc,
null,
null);
dialog.setVisible(true);
System.out.println( cc.getColor() );

关于java - 在 JColorChooser 中仅显示 2 个面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34616109/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com