繁体   English   中英

使用 JColorChooser 更改 JPanel 颜色

[英]Change a JPanel color using the JColorChooser

我试图在按下“应用”按钮时使用 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());


}

这是通过单击按钮更改JPanel背景颜色的简短演示。
这个 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();
    }
}

试试这个简单的源代码:

颜色选择器

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;
    }

}

我将假设“应用”按钮是指 JColorChooser 对话框的“确定”按钮。 在这种情况下,这是一个最佳解决方案:

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,这意味着它可以作为组件添加到框架中,让您可以进行控制,例如添加 ChangeListeners 以即时检测颜色变化。 您可能会觉得有用的另一种方法是:

    JDialog jd=JColorChooser.createDialog(Component c, 
                                          String title, 
                                          boolean modal, 
                                          JColorChooser chooserPane, 
                                          ActionListener okListener, 
                                          ActionListener cancelListener);
                                          

其中c是父组件 - 通常将其保留为空。
标题是对话框标题模态是一个布尔值,它指定您是否希望程序在继续程序线程执行之前等待用户响应对话框。

chooserPane 是您想要作为主要选择器的 JColorChooser->例如:

new JColorChooser(Color.GREEN);

okListenercancelListener是 ok 和 cancel 按钮的动作监听器。 此方法使您可以控制对话框按钮、显示等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM