使用 JColorChooser 设置 JButton 的前景色和背景色

Set foreground and background color of JButton with JColorChooser

我有一个简单的应用程序,它由一个 JButton 和一个 JColorChooser 组成。 当我按下按钮 2 JColorChoosers appear.The 时,一个允许我 select 背景色,第二个允许我 select 前景色。 我将每种颜色单独保存 variable.Here 是我的代码:

    public class Slide extends JFrame{

        Color bgColor;
        JButton colorButton=new JButton();
        JColorChooser colorPicker=new JColorChooser();
        public Slide(){
            JPanel panel=new JPanel();
            panel.setLayout(new MigLayout("", "[][][][][]", "[][][][][][]"));
            this.setContentPane(panel);
            colorButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    //1st color chooser for background color
                    bgColor=JColorChooser.showDialog(null, "Background color", null);
//2nd colorchooser appears for foreground
                    Color fgColor=JColorChooser.showDialog(null, "Foreground color", null);
                    colorButton.setBackground(bgColor);
                    colorButton.setForeground(fgColor);
                }
            });
            colorButton.setText("Pick a color");
            panel.add(colorButton, "cell 0 5");
            this.setSize(400, 400);
            this.setVisible(true);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        public static void main(String args[]){
            new Slide();
        }
    }

现在我的问题 is:Is 可以最小化 JColorChooser.I 的部分意味着我只想显示一次 JColorChooser 并从用户那里获得前景色和背景色

我认为您不能为此使用 JOptionPane,因为在您按下按住选项的按钮后对话框会立即消失。


您可以制作自己的 JDialog,中间有一个 JColorChooser,底部有一些按钮,例如 Set ForegroundSet Background;这样你就不必调用 2 JColorChoosers:

代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example {

    public Example() {

        JButton button = new JButton("Button");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                CustomColorChooserDialog dialog = new CustomColorChooserDialog(button);
                dialog.setVisible(true);
            }
        });

        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }
}

class CustomColorChooserDialog extends JDialog {

    JComponent targetComponent;
    JColorChooser colorChooser;
    JButton backgroundButton;
    JButton foregroundButton;
    JButton okButton;

    public CustomColorChooserDialog(JComponent targetComponent) {

        this.targetComponent = targetComponent;

        colorChooser = new JColorChooser();

        ButtonActionListener listener = new ButtonActionListener();

        backgroundButton = new JButton("Set Background");
        backgroundButton.addActionListener(listener);

        foregroundButton = new JButton("Set Foreground");
        foregroundButton.addActionListener(listener);

        okButton = new JButton("OK");
        okButton.addActionListener(listener);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(backgroundButton);
        buttonPanel.add(foregroundButton);
        buttonPanel.add(okButton);

        getContentPane().add(colorChooser, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.PAGE_END);

        pack();
        setModal(true);
        setLocationRelativeTo(targetComponent);

    }

    private class ButtonActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(backgroundButton)) {
                targetComponent.setBackground(colorChooser.getColor());
            } else if (e.getSource().equals(foregroundButton)) {
                targetComponent.setForeground(colorChooser.getColor());
            } else if (e.getSource().equals(okButton)) {
                dispose();
            }
        }
    }

}