简体   繁体   English

更改 JColorChooser 的预览面板

[英]Change Preview Panel for JColorChooser

The objective is to change the Preview section from this:目标是改变预览部分:

在此处输入图像描述

To something like this where the Preview area has a border, a solid box in the background and "Hello World" string which changes to the color selected for the Preview.对于这样的情况,预览区域有一个边框,背景中有一个实心框和“Hello World”字符串,该字符串会更改为为预览选择的颜色。

在此处输入图像描述

Started with this sample from java2 which simply shows the JColorChooser.从 java2 中的这个示例开始,它简单地显示了 JColorChooser。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

public class ColorChooserSample {
  public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    final JButton button = new JButton("Pick to Change Background");

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Color initialBackground = button.getBackground();
        JColorChooser jColorChooser = new JColorChooser();
        jColorChooser.setPreviewPanel(null);
        Color background = jColorChooser.showDialog(null,
            "JColorChooser Sample", initialBackground);
        if (background != null) {
          button.setBackground(background);
        }
      }
    };
    button.addActionListener(actionListener);
    content.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}

Just to see if I can even affect the Preview area, I modified the original code from this:只是为了看看我是否能影响预览区域,我修改了原来的代码:

    Color background = JColorChooser.showDialog(null,
        "JColorChooser Sample", initialBackground);

To this:对此:

        JColorChooser jColorChooser = new JColorChooser();
        jColorChooser.setPreviewPanel(null);
        Color background = jColorChooser.showDialog(null,
            "JColorChooser Sample", initialBackground);

Basically this is to attempt to see if the Preview section could be null (blank) but it had no affect which has me wondering if the setPreviewPanel() is the correct call.基本上这是试图查看预览部分是否可以是 null(空白),但它没有影响,这让我想知道 setPreviewPanel() 是否是正确的调用。

Also, after the code change this Warning appears: The static method showDialog(Component, String, Color) from the type JColorChooser should be accessed in a static way此外,在代码更改后会出现此警告: JColorChooser 类型的 static 方法 showDialog(Component, String, Color) 应该以 static 方式访问

Questions:问题:

Are there any examples which alter the preview section of the Color Chooser?有没有改变颜色选择器预览部分的例子?

Why did the null above not work.为什么上面的null不起作用。

If the Warning indicates that JColorChooser should be accessed in a static way, how would one actually make a setPreviewPanel() call?如果警告表明应该以 static 方式访问 JColorChooser,那么实际上如何进行 setPreviewPanel() 调用?

There weren't many examples of modifying the JColorChooser preview panel.修改JColorChooser预览面板的例子并不多。 I found four examples.我找到了四个例子。 Along with the Javadoc for JColorChooser , I created a working example of a modified JColorChooser preview panel.除了JColorChooser的 Javadoc 之外,我还创建了一个修改后的JColorChooser预览面板的工作示例。

JColorChooser 示例 GUI 1

I modified the original JFrame / JPanel to change the background color of the JPanel , so it would be easier to see the result.我修改了原来的JFrame / JPanel来改变JPanel的背景颜色,这样更容易看到结果。

JColorChooser 示例 GUI 2

Here's the modified JColorChooser .这是修改后的JColorChooser

JColorChooser 示例 GUI 3

Here I changed the background color of the preview JPanel to yellow.在这里,我将预览JPanel的背景颜色更改为黄色。

JColorChooser 示例 GUI 4

Which then changes the background color of the main JPanel to yellow.然后将主JPanel的背景颜色更改为黄色。

Here's the code.这是代码。 I separated the code into bite-sized methods and classes, so I could concentrate on one part of the GUI data a time.我将代码分成小块的方法和类,这样我就可以一次专注于 GUI 数据的一部分。

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

    @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!");
            previewPanel.add(label);
            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);
        }

    }

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

    }

}

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

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