简体   繁体   English

如何覆盖 JColorChooser 重置按钮?

[英]How to override JColorChooser Reset Button?

In a previous post, I discussed overriding the Preview panel and all is working well, except a new question has come forth and hoping the fix for it is as easy.在上一篇文章中,我讨论了覆盖预览面板并且一切正常,除了出现了一个新问题并希望修复它同样简单。

This new question pertains to the Reset button.这个新问题与重置按钮有关。 Based on the documentation, the Reset button will reset the Colors back to the original color that was passed in. This works great, but what if there is a secondary field?根据文档,重置按钮会将 Colors 重置为传入的原始颜色。这很好用,但是如果有辅助字段怎么办?

Here is the code...这是代码...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ColorChooserSample implements Runnable{

    public static void main(String args[]) {
        SwingUtilities.invokeLater(
                new ColorChooserSample());
    }

    private JPanel panel;
    JTextField counter;
    int vCounter = 1;

    @Override
    public void run() {
        JFrame frame = new JFrame(
                "JColorChooser Sample");
        frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(300, 200));
        JButton button = new JButton(
                "Pick to Change JPanel Background");
        button.addActionListener(new ColorListener());
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public void setJPanelBackground(Color color) {
        panel.setBackground(color);
        panel.repaint();
    }

    public class ColorListener implements 
            ActionListener, ChangeListener {

        private JColorChooser chooser;

        private JPanel previewPanel;

        @Override
        public void actionPerformed(
                ActionEvent actionEvent) {
            Color backgroundColor = showDialog(panel, 
                    "Set JPanel Background", 
                    panel.getBackground());
            setJPanelBackground(backgroundColor);
        }

        private Color showDialog(Component component, 
                String title, Color initialColor) 
                        throws HeadlessException {
            chooser = new JColorChooser(initialColor);
            chooser.getSelectionModel()
                .addChangeListener(this);

            // configuring color chooser panel
            previewPanel = new JPanel();
            previewPanel.setBackground(initialColor);
            JLabel label = new JLabel("Hello World!");
            counter = new JTextField("0");
            previewPanel.add(label, BorderLayout.WEST);
            previewPanel.add(counter, BorderLayout.EAST);
            chooser.setPreviewPanel(previewPanel);

            // creating dialog
            ColorTracker ok = new ColorTracker(chooser);
            JDialog dialog = JColorChooser.createDialog(
                    component, title, true, chooser, 
                    ok, null);
            dialog.setVisible(true);
            return ok.getColor();
        }

        @Override
        public void stateChanged(ChangeEvent event) {
            Color newColor = chooser.getColor();
            previewPanel.setBackground(newColor);
            counter.setText(Integer.toString(vCounter++));
        }
    }

    private class ColorTracker implements ActionListener {

        private Color color;

        private JColorChooser chooser;

        public ColorTracker(JColorChooser chooser) {
            this.chooser = chooser;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            color = chooser.getColor();
        }

        public Color getColor() {
            return color;
        }

    }

}

The above code will first open this dialog.上面的代码将首先打开这个对话框。

在此处输入图像描述

Once the "Pick to Change JPanelBackground" button is clicked, it opens the JColorDialog with a modified Preview section.单击“Pick to Change JPanelBackground”按钮后,它将打开带有修改后的预览部分的 JColorDialog。 This section includes "Hello World" that will change background colors and a counter to count how many times the color has changed.此部分包括将更改背景 colors 的“Hello World”和一个用于计算颜色更改次数的计数器。

在此处输入图像描述

The following shows the last color selected but also shows 5 in the Textbox to signify that there were 5 color options clicked.下面显示了最后选择的颜色,但在文本框中也显示了 5,表示单击了 5 个颜色选项。

在此处输入图像描述

Click the reset button and the color background is set to the original, but the count was NOT reset back to 0.单击重置按钮,颜色背景设置为原始颜色,但计数未重置为 0。

在此处输入图像描述

You can pass in a Listener for both the OK and Cancel button but not the Reset button.您可以为“确定”和“取消”按钮传入一个侦听器,但不能为“重置”按钮传入一个侦听器。

This is only an example, as there could be other items in the Preview section.这只是一个示例,因为预览部分中可能还有其他项目。 The objective is how can the values other than Color be reset?目标是如何重置颜色以外的值?

The only thing done by reset button is calling chooserPane.setColor(initialColor) . reset按钮所做的唯一事情是调用chooserPane.setColor(initialColor) So if what you really need is listening to color changes, you might do just that, by something like:因此,如果您真正需要的是聆听颜色变化,您可以这样做,例如:

        chooser.getSelectionModel().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                Color newColor = chooser.getColor();
                previewPanel.setBackground(newColor);
                counter.setText(Integer.toString(vCounter++));
            }
        });

And your secondary panel gets updated.并且您的辅助面板会更新。

Having said that, if you really want to add a listener to the button, there's no exposed way of accessing it - you need to iterate the components all the way down until you find the button, and then add a listener to it.话虽如此,如果您真的想为按钮添加侦听器,则没有公开的访问方式 - 您需要一直迭代组件直到找到按钮,然后向其添加侦听器。

EDIT Alright, so it has to be the reset button.编辑好的,所以它必须是重置按钮。 Since that's a local variable of some content creating method, it has to be removed surgically.由于这是某些内容创建方法的局部变量,因此必须通过手术将其删除。 Just change new JButton("abc") to your own button, with your own listener(s).只需使用您自己的侦听器将new JButton("abc")更改为您自己的按钮。

        Locale locale = dialog.getLocale();
        String resetString = UIManager.getString("ColorChooser.resetText", locale);

        Container contentPane = dialog.getContentPane();
        JPanel buttonPanel = null;
        for (Component c : contentPane.getComponents()) {
            if (c instanceof JPanel) {
                buttonPanel = (JPanel) c;
            }
        }

        JButton resetButton = null;
        if (buttonPanel != null) {
            for (Component b : buttonPanel.getComponents()) {
                if (b instanceof JButton) {
                    JButton button = (JButton) b;
                    if (resetString.equals(button.getText())) {
                        resetButton = button;
                        break;
                    }
                }
            }
            if (resetButton != null) {
                buttonPanel.remove(resetButton);
                buttonPanel.add(new JButton("abc"));
            }
        }

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

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