How can I change the color of the background image that is black to the color that is selelected?
JComponent colorBar = new ColorComponent(Color.BLACK);
colorBar.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Color defaultColor = getBackground();
Color selected = JColorChooser.showDialog(DessinFrame.this,
"Set background", defaultColor);
if (selected != null) setBackground(selected);
}
});
Close. It looks like your get/setBackground(selected) should be referencing the colorBar component
JComponent colorBar = new ColorComponent(Color.BLACK);
colorBar.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Color defaultColor = colorBar.getBackground();
Color selected = JColorChooser.showDialog(DessinFrame.this, "Set background", defaultColor);
if (selected != null) colorBar.setBackground(selected);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.