Java 如何在JColorChooser中增加样例的大小?
JColorChooser/Swatches中调色板的大小非常小,而且调色板周围的颜色看起来也很相似 对于我的使用,我需要增加他们的大小以及改变颜色的变化。如何做到这一点?Java 如何在JColorChooser中增加样例的大小?,java,swing,jcolorchooser,Java,Swing,Jcolorchooser,JColorChooser/Swatches中调色板的大小非常小,而且调色板周围的颜色看起来也很相似 对于我的使用,我需要增加他们的大小以及改变颜色的变化。如何做到这一点? [JColorChooser][1]是复合JComponent 您可以选择仅显示/使用JColorChooser 为了更容易地使用变通方法 import java.awt.*; 导入java.awt.event.*; 导入javax.swing.*; 导入javax.swing.colorchooser.Abstract
是复合JComponent[JColorChooser][1]
- 您可以选择仅显示/使用
JColorChooser
- 为了更容易地使用变通方法
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.colorchooser.AbstractColorChooserPanel;
导入javax.swing.event.*;
导入javax.swing.plaf.metal.metalcomboxIcon;
公共类下拉组件2{
私人JWindow(窗口);;
私有布尔值_windowShouldBeShown=false;
专用JComponent_组件;
私有抽象按钮_按钮;
私有JFrame _ownerFrame;
公共下拉组件2(JFrame-ownerFrame、JComponent组件、AbstractButton按钮){
_ownerFrame=ownerFrame;
_组件=组件;
_按钮=按钮;
_addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
_window.setVisible(false);
点pt=_按钮。getLocationOnScreen();
平移(0,_button.getHeight());
_窗口设置位置(pt);
showWindow();
_windowShouldBeShown=true;
Window[]wins=Window.getWindows();
System.out.println(wins.length);
}
});
_addAncestorListener(新的AncestorListener(){
@凌驾
添加了公共无效的AncestorEvent(AncestorEvent事件){
_window.setVisible(false);
}
@凌驾
已删除公共无效的AncestorEvent(AncestorEvent事件){
_window.setVisible(false);
}
@凌驾
公共无效取消删除(取消存储事件){
if(event.getSource()!=\u窗口){
System.out.println(“Ansestor移动”);
_window.setVisible(false);
}
}
});
Toolkit.getDefaultToolkit().addAWTEventListener(
新的AWTEventListener(){
@凌驾
已调度的公共void事件(AWTEvent事件){
如果(event.getID()==MouseEvent.MOUSE_单击){
如果(!\u window.getBounds()包含(MouseInfo.getPointerInfo().getLocation()){
如果(_windowShouldBeShown){
_windowShouldBeShown=false;
}否则{
_window.setVisible(false);
}
}
}
}
},AWTEvent.MOUSE\u EVENT\u MASK);
_窗口=新的JWindow(_ownerFrame);
_window.getContentPane().add(组件);
_addWindowFocusListener(新的WindowAdapter(){
@凌驾
公共无效windowLostFocus(WindowEvent evt){
System.out.println(“窗口失焦”);
_window.setVisible(false);
}
});
_window.pack();
}
私有矩形getScreenRect(){
返回新矩形(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
}
公共void showWindow(){
矩形screenRect=getScreenRect();
矩形windowRect=_window.getBounds();
int sx1=screenRect.x;
int sx2=screenRect.x+screenRect.width;
int sy1=屏幕矩形y;
int sy2=screenRect.y+screenRect.height;
int wx1=windowRect.x;
int wx2=windowRect.x+windowRect.width;
int wy1=windowRect.y;
int wy2=windowRect.y+windowRect.height;
如果(wx2>sx2){
_setLocation(wx1-(wx2-sx2),_window.getY());
}
if(wx1sy2){
_setLocation(_window.getX(),wy1-(wy2-wy1));
}
if(wy2import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.event.*;
import javax.swing.plaf.metal.MetalComboBoxIcon;
public class DropDownComponent2 {
private JWindow _window;
private boolean _windowShouldBeShown = false;
private JComponent _component;
private AbstractButton _button;
private JFrame _ownerFrame;
public DropDownComponent2(JFrame ownerFrame, JComponent component, AbstractButton button) {
_ownerFrame = ownerFrame;
_component = component;
_button = button;
_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_window.setVisible(false);
Point pt = _button.getLocationOnScreen();
pt.translate(0, _button.getHeight());
_window.setLocation(pt);
showWindow();
_windowShouldBeShown = true;
Window[] wins = Window.getWindows();
System.out.println(wins.length);
}
});
_button.addAncestorListener(new AncestorListener() {
@Override
public void ancestorAdded(AncestorEvent event) {
_window.setVisible(false);
}
@Override
public void ancestorRemoved(AncestorEvent event) {
_window.setVisible(false);
}
@Override
public void ancestorMoved(AncestorEvent event) {
if (event.getSource() != _window) {
System.out.println("Ansestor moved");
_window.setVisible(false);
}
}
});
Toolkit.getDefaultToolkit().addAWTEventListener(
new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event.getID() == MouseEvent.MOUSE_CLICKED) {
if (!_window.getBounds().contains(MouseInfo.getPointerInfo().getLocation())) {
if (_windowShouldBeShown) {
_windowShouldBeShown = false;
} else {
_window.setVisible(false);
}
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
_window = new JWindow(_ownerFrame);
_window.getContentPane().add(component);
_window.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowLostFocus(WindowEvent evt) {
System.out.println("window lost focus");
_window.setVisible(false);
}
});
_window.pack();
}
private Rectangle getScreenRect() {
return new Rectangle(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
}
public void showWindow() {
Rectangle screenRect = getScreenRect();
Rectangle windowRect = _window.getBounds();
int sx1 = screenRect.x;
int sx2 = screenRect.x + screenRect.width;
int sy1 = screenRect.y;
int sy2 = screenRect.y + screenRect.height;
int wx1 = windowRect.x;
int wx2 = windowRect.x + windowRect.width;
int wy1 = windowRect.y;
int wy2 = windowRect.y + windowRect.height;
if (wx2 > sx2) {
_window.setLocation(wx1 - (wx2 - sx2), _window.getY());
}
if (wx1 < sx1) {
_window.setLocation(0, _window.getY());
}
if (wy2 > sy2) {
_window.setLocation(_window.getX(), wy1 - (wy2 - wy1));
}
if (wy2 < sy1) {
_window.setLocation(_window.getX(), 0);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_window.setVisible(true);
}
});
}
public void hideWindow() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_window.setVisible(false);
}
});
}
}
class DropDownFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JButton _button;
private JColorChooser _colorChooser;
private DropDownComponent2 _dropDown;
private JWindow _window;
public DropDownFrame() {
_colorChooser = new JColorChooser();
_colorChooser.setPreviewPanel(new JPanel());
_colorChooser.setColor(Color.RED);
// Remove panels other than Swatches
AbstractColorChooserPanel[] panels = _colorChooser.getChooserPanels();
for (int i = 0; i < panels.length; i++) {
if (!panels[i].getDisplayName().equals("Swatches")) {
_colorChooser.removeChooserPanel(panels[i]);
} else {
JPanel panel = panels[i];
System.out.println(panel.getComponentCount()); // 1
System.out.println(panel.getPreferredSize()); //width=424,height=112
System.out.println(panel.getLayout()); //FlowLayout[hgap=5,vgap=5,align=center]
}
}
_colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override // ### I think the key point is there
public void stateChanged(ChangeEvent e) {
_button.setForeground(_colorChooser.getColor());
_dropDown.hideWindow();
}
});
_button = new JButton("Show JWindow");
_button.setIcon(new MetalComboBoxIcon());
_button.setHorizontalTextPosition(SwingConstants.LEFT);
this.getContentPane().add(_button);
_dropDown = new DropDownComponent2(DropDownFrame.this, _colorChooser, _button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DropDownFrame dropDownFrame = new DropDownFrame();
}
});
}
}
int length = 20;
UIManager.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(length, length));
UIManager.put("ColorChooser.swatchesSwatchSize", new Dimension(length, length));
JColorChooser chooser = new JColorChooser();
List<AbstractColorChooserPanel> choosers =
new ArrayList<>(Arrays.asList(chooser.getChooserPanels()));
choosers.remove(0);
MySwatchChooserPanel swatch = new MySwatchChooserPanel();
choosers.add(0, swatch);
chooser.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0]));