gpt4 book ai didi

java - 如何从 JColorChooser 获取颜色 Pane 并在我自己的窗口中使用它?

转载 作者:行者123 更新时间:2023-12-01 19:57:42 25 4
gpt4 key购买 nike

我是 Java 和 Java swing 新手。

我只需要提取颜色 Pane 并从我自己的窗口中的该 Pane 获取颜色。

基本上,我想删除默认 ColorPanel 中其他不必要的组件。我成功地移除了其他面板和 slider 。但没有进一步。

JColorChooser

最佳答案

这很困难,需要一些 AWT/Swing hack(例如 Robot 类),但也是可能的。我不建议您使用此代码,但如果需要的话......

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* <code>ChooserTryout</code>.
*/
public class ChooserTryout {

public static void main(String[] args) {
try {
Robot robot = new Robot();
SwingUtilities.invokeLater(() -> new ChooserTryout().startUI(robot));
} catch (AWTException e) {
e.printStackTrace();
}
}

public void startUI(Robot robot) {
JFrame frm = new JFrame("Color test");
JColorChooser chooser = new JColorChooser();
JLabel label = new JLabel("Here is your actual color");
label.setOpaque(true);
List<JComponent> comps =
getAllChildrenOfClass(chooser, JComponent.class, c -> c.getClass().getName().contains("DiagramComponent"));
JComponent c = comps.get(3);
c.setPreferredSize(new Dimension(300, 300));
c.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Point p = c.getLocationOnScreen();
p.translate(e.getX(), e.getY());
Color color = robot.getPixelColor(p.x, p.y);
System.out.println("You've clicked color: " + color);
label.setForeground(color);
};
});
frm.add(c);
frm.add(label, BorderLayout.SOUTH);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}

/**
* Searches for all children of the given component which are instances of the given class and satisfies the given condition.
*
* @param aRoot start object for search. May not be null.
* @param aClass class to search. May not be null.
* @param condition condition to be satisfied. May not be null.
* @param <E> class of component.
* @return list of all children of the given component which are instances of the given class. Never null.
*/
public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass, Predicate<E> condition) {
final List<E> result = new ArrayList<>();
final Component[] children = aRoot.getComponents();
for (final Component c : children) {
if (aClass.isInstance(c) && condition.test(aClass.cast(c))) {
result.add(aClass.cast(c));
}
if (c instanceof Container) {
result.addAll(getAllChildrenOfClass((Container) c, aClass, condition));
}
}
return result;
}

}

关于java - 如何从 JColorChooser 获取颜色 Pane 并在我自己的窗口中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59026643/

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