[英]Change a JPanel color using the JColorChooser
Im trying to change the color of a JPanel using the JColorChooser when the "apply" button is pressed, but i'm not sure how to actually make the color change.我试图在按下“应用”按钮时使用 JColorChooser 更改 JPanel 的颜色,但我不确定如何实际更改颜色。 How would I do that?
我该怎么做?
private class SetColorAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
setColor(DrawnView.colorChooser.getColor());
//Color color;
}
}
^ is one in class while the stuff below is in a different one ^ 是课堂上的一个,而下面的东西是另一个
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());
}
Here is a short demo of changing the background color of a JPanel
by a button click.这是通过单击按钮更改
JPanel
背景颜色的简短演示。
This cam also give you an idea of mcve , doing just that and nothing more:这个 cam 还可以让您了解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();
}
}
Try this simple source code:试试这个简单的源代码:
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;
}
}
I am going to assume by the "apply" button, you mean the "ok" button of the JColorChooser dialog.我将假设“应用”按钮是指 JColorChooser 对话框的“确定”按钮。 In that case, here is an optimal solution:
在这种情况下,这是一个最佳解决方案:
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); }
This should do the trick, but here's my advice for you.这应该可以解决问题,但这是我对您的建议。 JColorChooser extends JComponent, and this means that IT CAN BE ADDED AS A COMPONENT to a Frame, giving you control such as adding ChangeListeners to instantaneously detect a Color change.
JColorChooser 扩展了 JComponent,这意味着它可以作为组件添加到框架中,让您可以进行控制,例如添加 ChangeListeners 以即时检测颜色变化。 Another method you may find helpful is:
您可能会觉得有用的另一种方法是:
JDialog jd=JColorChooser.createDialog(Component c,
String title,
boolean modal,
JColorChooser chooserPane,
ActionListener okListener,
ActionListener cancelListener);
Where c
is the parent component-leave it null, usually.其中
c
是父组件 - 通常将其保留为空。
The title is the dialogs Title modal is a boolean value which specifies whether you want the program to wait for the the dialog to be responded to by the user before continuing the program thread execution.标题是对话框标题模态是一个布尔值,它指定您是否希望程序在继续程序线程执行之前等待用户响应对话框。
chooserPane is the JColorChooser you want as the main chooser->eg: chooserPane 是您想要作为主要选择器的 JColorChooser->例如:
new JColorChooser(Color.GREEN);
okListener
and cancelListener
are action listeners for ok and cancel buttons. okListener
和cancelListener
是 ok 和 cancel 按钮的动作监听器。 This method gives you control over the dialogs buttons, display, etc.此方法使您可以控制对话框按钮、显示等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.