当按下"apply“按钮时,我正在尝试使用JColorChooser更改JPanel的颜色,但我不确定如何实际更改颜色。我该怎么做呢?
private class SetColorAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
setColor(DrawnView.colorChooser.getColor());
//Color color;
}
}^是班级中的一个,而下面的内容是不同的
public void setColor(Color color){
this.setBackground(color);
}
public ViewUserActions() {
this.applyColorBtn.setVisible(false);
this.discardChangesBtn.setVisible(false);
this.editBtn.addActionListener((ActionEvent ae) -> {
if (this.editBtn.isSelected()) {
this.applyColorBtn.setVisible(true);
this.discardChangesBtn.setVisible(true);
} else {
this.applyColorBtn.setVisible(false);
this.discardChangesBtn.setVisible(false);
}
});
this.applyColorBtn.addActionListener(new SetColorAction());
this.discardChangesBtn.addActionListener(new SetColorAction());
this.applyColorBtn.addActionListener(new GetInfoAction());
this.discardChangesBtn.addActionListener(new GetInfoAction());
}发布于 2018-01-18 14:07:44
下面是一个简短的演示,演示如何通过单击按钮来更改JPanel的背景颜色。
这也可以让你对mcve有一个概念,只做这些,没有更多的东西:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Frame extends JFrame {
JPanel panel;
Color[] colors = new Color[] {Color.YELLOW, Color.CYAN, Color.LIGHT_GRAY, Color.WHITE};
int counter =0;
Frame() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JButton button = new JButton("Change color");
button.addActionListener( ae -> setColor());
add(button, BorderLayout.NORTH);
panel = new JPanel();
panel.add(new JLabel ("Test panel"));
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
private void setColor() {
panel.setBackground(colors[counter++]);
counter = (counter >= colors.length) ? 0 : counter;
}
public static void main(String[] args) {
new Frame();
}
}发布于 2018-01-18 14:39:03
试试这个简单的源代码:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ColorChooserExample {
private static JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
runColorChangerApp();
}
});
}
private static void runColorChangerApp() {
frame = new JFrame();
frame.setTitle("JPanel Color Changer");
frame.getContentPane().setLayout(new GridLayout(1, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 250, 400, 300);
frame.getContentPane().add(getHomePanel());
frame.setVisible(true);
}
private static JPanel getHomePanel() {
final JPanel panel = new JPanel();
panel.setOpaque(true);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
//Fire on Mouse Right Click
if(evt.getButton() == MouseEvent.BUTTON3) {
frame.setTitle("Listened Right Click");
Color initColor = panel.getBackground();
Color choosedColor = JColorChooser.showDialog(panel,
"Choose JPanel Background Color", initColor);
frame.setTitle("JPanel Color Changer");
panel.setBackground(choosedColor);
}
}
});
return panel;
}
}发布于 2020-10-16 13:42:11
我假设"apply“按钮是指JColorChooser对话框的"ok”按钮。在这种情况下,以下是一个最佳解决方案:
Color r = JColorChooser.showDialog(null, "Select Color for JPanel", Color.CYAN);
//null is the parent Component for the dialog,The String is the title, and cyan
//will be the initially selected Color.
if(r==null)
{ //perform whatever you would do if the user cancels the dialog }
else
{ jpanelobj.setBackground(r); }这应该会起作用,但这是我给你的建议。JColorChooser扩展了JComponent,这意味着IT可以作为A组件添加到帧中,让您可以控制,例如添加ChangeListeners以即时检测颜色变化。你可能会发现另一种有用的方法是:
JDialog jd=JColorChooser.createDialog(Component c,
String title,
boolean modal,
JColorChooser chooserPane,
ActionListener okListener,
ActionListener cancelListener);其中c是父组件-通常将其保留为空。
标题是对话框标题模式是一个布尔值,它指定您是否希望程序在继续执行程序线程之前等待用户对对话框作出响应。
chooserPane是您希望作为主要选择器的JColorChooser ->例如:
new JColorChooser(Color.GREEN);okListener和cancelListener是ok和cancel按钮的动作监听器。此方法使您可以控制对话框按钮、显示等。
https://stackoverflow.com/questions/48311038
复制相似问题