gpt4 book ai didi

java - 如何在其他类中使用 JColorChooser 给定颜色?

转载 作者:行者123 更新时间:2023-11-30 03:14:28 35 4
gpt4 key购买 nike

我学习 Java 才几周时间,所以请记住这一点..我自己已经成功解决了许多问题,直到这个:

我想要实现的是在选项卡式 View 中使用 JComponent,以便在一个选项卡中您可以选择一种颜色(我的 println 显示它实际上获得了 sRGB 值)。然后在另一个类中我应该能够获取这个值并将其用于为 JPanel 着色。我可以只传递 Color 对象吗?或者实现的最佳方法是什么?我在这里尝试的效果不太好。抱歉,代码很乱 - 我是新手......这是颜色选择器的主要部分:

public class Colors extends JPanel implements ChangeListener {

private JColorChooser jcc = null;
protected JLabel title;
static Color newColor;

public Colors() {
super(new BorderLayout());

//Set up the banner at the top of the window
title = new JLabel();

//Set up color chooser for setting text color
jcc = new JColorChooser(title.getForeground());
jcc.getSelectionModel().addChangeListener(this);
jcc.setBorder(BorderFactory.createTitledBorder(
"Choose New Color"));

AbstractColorChooserPanel[] panels=jcc.getChooserPanels();
for(AbstractColorChooserPanel p:panels) {
String displayName = p.getDisplayName();
switch (displayName) {
case "HSV":
jcc.removeChooserPanel(p);
break;
case "HSL":
jcc.removeChooserPanel(p);
break;
case "CMYK":
jcc.removeChooserPanel(p);
break;
case "RGB":
jcc.removeChooserPanel(p);
break;
}
}
add(jcc, BorderLayout.PAGE_END);
}



public void stateChanged(ChangeEvent e) {
Color newColor = jcc.getColor();
title.setForeground(newColor);
System.out.println("color = " + newColor);
}

public static Color getNewCol() {
System.out.println("this now =" + newColor);
return newColor;
}

public static Component createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Color Selector");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new Colors();
return newContentPane;

}
}

然后我尝试以这种方式获取 Main.Java 类中所选的颜色(这可能是错误的)。我还注意到该值似乎始终保持为空 - 可能我以错误的方式实例化(值丢失了?)

a.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Click add");
value++;

if (value < 21) {


JButton jb1 = new JButton();

//How to get the COLOR from the JColorChooser class?
Color box = Colors.getNewCol();

jb1.setBackground(box);
jb1.setOpaque(true);

//FOR TEST ONLY jb1.setBackground(Color.BLUE);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(jb1);
panel.setVisible(true);

//window.add(paneeli);

window.pack();

d = new Dimension(700, 500);
window.setSize(d);

可能答案太明显了,但我现在看不到。

最佳答案

我会获取你的静态 newColor 变量并使其成为非静态的。在我的 ChangeListener 中,我将触发 JPanel 固有的 PropertyChangeSupport,以便可以通知监听器。关键是使用观察者设计模式——例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.*;

@SuppressWarnings("serial")
public class TestColors extends JPanel {
private JTabbedPane tabbedPane = new JTabbedPane();
private Colors colors = new Colors();
private JPanel colorDisplayPanel = new JPanel();

public TestColors() {
tabbedPane.add("Colors", colors);
tabbedPane.add("Color Display", colorDisplayPanel);
setLayout(new BorderLayout());
add(tabbedPane);

// add a PropertyChangeListener to our Colors isntance.
colors.addPropertyChangeListener(Colors.NEW_COLOR,
new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent evt) {
Color color = (Color) evt.getNewValue();
colorDisplayPanel.setBackground(color);
}
});
}

private static void createAndShowGui() {
JFrame frame = new JFrame("TestColors");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestColors());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

@SuppressWarnings("serial")
class Colors extends JPanel implements ChangeListener {
public static final String NEW_COLOR = "new color";
private JColorChooser jcc = null;
protected JLabel title;
private Color newColor = null;

public Colors() {
super(new BorderLayout());

// Set up the banner at the top of the window
title = new JLabel("This is my Banner!", SwingConstants.CENTER);

// Set up color chooser for setting text color
jcc = new JColorChooser(title.getForeground());
jcc.getSelectionModel().addChangeListener(this);
jcc.setBorder(BorderFactory.createTitledBorder("Choose New Color"));

add(jcc, BorderLayout.CENTER);
add(title, BorderLayout.PAGE_START);
}

public void stateChanged(ChangeEvent e) {
// Color newColor = jcc.getColor();
Color oldValue = newColor;
newColor = jcc.getColor();

// fire a notification to the Colors JPanel's property change support
// object. Any listeners will be notified of the color change
firePropertyChange(NEW_COLOR, oldValue, newColor);
title.setForeground(newColor);
}

public Color getNewCol() {
System.out.println("this now =" + newColor);
return newColor;
}

}

关于java - 如何在其他类中使用 JColorChooser 给定颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32953974/

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