简体   繁体   English

如何在我的GUI上显示我的颜色图标?

[英]How can I get my Color Icon to display on my GUI?

I'm trying to create a GUI where I can create custom colors. 我正在尝试创建一个GUI,我可以创建自定义颜色。 I have a feature that lets the user preview the color before submitting the color. 我有一个功能,让用户在提交颜色之前预览颜色。 I can't get the Icon to display at all. 我根本无法显示图标。 The actual problem is in my ColorFrame class. 实际问题出在我的ColorFrame类中。 The color Icon(a JLabel, color) is created in the actionperformed() method. 颜色图标(JLabel,颜色)在actionperformed()方法中创建。

The code that creates the Icon(The ButtonListener class): 创建Icon的代码(ButtonListener类):

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ColorFrame extends JFrame implements ActionListener {

private JPanel colorPanel;
private JPanel labelPanel;
private JPanel buttonPanel;

private JLabel labelRed;
private JLabel labelGreen;
private JLabel labelBlue;
private JLabel color;

private JTextField redField;
private JTextField greenField;
private JTextField blueField;

private JButton preview;
private JButton submit;


private int redInt;
private int blueInt;
private int greenInt;


private int width = 30;
private int height = 30;

public ColorFrame(int x, int y)
{
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes all frames for some reason
    this.pack();
    this.setLocation(x, y);

    buttonPanel = new JPanel();
    this.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
    preview = new JButton("Preview Color");
    preview.addActionListener(new ButtonListener());
    buttonPanel.add(preview);
    createColorPanel();
    createLabelPanel();

    this.setVisible(true);

}

class ButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent arg0) {
        //in the gui, there are three jtextfieilds that represent color values. the first is red, the second is green, and the last is blue.
        redInt = Integer.parseInt(redField.getText());
        greenInt = Integer.parseInt(greenField.getText());
        blueInt = Integer.parseInt(blueField.getText());

        color = new JLabel(new ColorIcon(redInt,greenInt,blueInt));
        buttonPanel.add(color,BorderLayout.SOUTH);
        print(redInt,greenInt, blueInt);

    }

}


private void createColorPanel()
{
    colorPanel = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    //creates the three textfields to input rgb values.
    //the first is r, second, b third g
    redField = new JTextField(2);
    c.fill = GridBagConstraints.VERTICAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    c.insets = new Insets(10,0,0,10);
    colorPanel.add(redField,c);

    greenField = new JTextField(2);
    c.fill = GridBagConstraints.VERTICAL;
    c.weightx = 0.0;
    c.gridx = 1;
    c.gridy = 1;
    colorPanel.add(greenField,c);

    blueField = new JTextField(2);
    c.fill = GridBagConstraints.VERTICAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 2;
    colorPanel.add(blueField,c);

    redField.addActionListener(this);
    greenField.addActionListener(this);
    blueField.addActionListener(this);



    this.add(colorPanel, BorderLayout.EAST);
}


public void actionPerformed(ActionEvent arg0) {
    redInt = Integer.parseInt(redField.getText());
    greenInt = Integer.parseInt(greenField.getText());
    blueInt = Integer.parseInt(blueField.getText());

    // creates the color icon. It works sometimes, but not every time.
    color = new JLabel(new ColorIcon(redInt,greenInt,blueInt));
    buttonPanel.add(color);
    print(redInt,greenInt, blueInt);

}


private void print(int a, int b, int c)
{
    // just to see if the actionperformed() method works.
    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
}


public Dimension getPreferredSize() {
    return new Dimension(300,300);
}


public static void main(String[] args)
{
    ColorFrame frame = new ColorFrame(200,200); 
}


}

Color Icon class: 颜色图标类:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.Icon;


public class ColorIcon implements Icon{



private final int size = 30;
private int red;
private int green;
private int blue;



public ColorIcon(int r, int g, int b)
{
    this.red = r;
    this.green = g;
    this.blue = b;
}


@Override
public int getIconHeight() {
    // TODO Auto-generated method stub
    return size;
}

@Override
public int getIconWidth() {
    // TODO Auto-generated method stub
    return size;
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    // TODO Auto-generated method stub

    Graphics2D g2 = (Graphics2D) g;
    Rectangle2D.Double square = new Rectangle2D.Double(50, 50, size, size);
    g2.setColor(new Color(red,green,blue));
    g2.fill(square);
}

}

There are a number of possible ways you can handle this. 您可以通过多种方式处理此问题。 One is to create a panel where you can set the color and paint the entire panel. 一种是创建一个面板,您可以在其中设置颜色并绘制整个面板。 Something like 就像是

private class ColorPanel extends JPanel {
    private Color color = Color.BLUE;

    public void setColor(Color color) {
        this.color = color;
        repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    public Dimension getPreferredSize() {
        return new Dimension(150, 150);
    }
}

You can just set the color. 你可以设置颜色。 Another thing I noticed is that you are trying to add the new label to the button panel, but the button panel is in the south of the frame. 我注意到的另一件事是你试图将新标签添加到按钮面板,但按钮面板位于框架的南边。 I think you want the label in the center of the frame. 我想你想要标签在框架的中心。 So you would add the new label to the CENTER by itself, and not to the button panel. 因此,您可以将新标签单独添加到CENTER,而不是按钮面板。

Here's a refactor of your code. 这是你的代码的重构。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class ColorFrame extends JFrame implements ActionListener {

    private JPanel colorPanel;
    private JPanel buttonPanel;

    private JLabel color;

    private JTextField redField;
    private JTextField greenField;
    private JTextField blueField;

    private JButton preview;

    private int redInt;
    private int blueInt;
    private int greenInt;

    private ColorPanel cPanel = new ColorPanel();

    public ColorFrame(int x, int y) {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buttonPanel = new JPanel();
        JPanel preferredSizeWrapper = new JPanel(new GridBagLayout());
        preferredSizeWrapper.add(cPanel);
        this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        preview = new JButton("Preview Color");
        preview.addActionListener(new ButtonListener());
        buttonPanel.add(preview);
        createColorPanel();
        this.add(preferredSizeWrapper);
        this.pack();
        this.setLocation(x, y);
        this.setVisible(true);

    }

    class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // in the gui, there are three jtextfieilds that represent color
            // values. the first is red, the second is green, and the last is
            // blue.
            redInt = Integer.parseInt(redField.getText());
            greenInt = Integer.parseInt(greenField.getText());
            blueInt = Integer.parseInt(blueField.getText());
            // new ColorIcon(redInt, greenInt, blueInt)
            cPanel.setColor(new Color(redInt, greenInt, blueInt));
        }
    }

    private class ColorPanel extends JPanel {
        private Color color = Color.BLUE;

        public void setColor(Color color) {
            this.color = color;
            repaint();
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        public Dimension getPreferredSize() {
            return new Dimension(150, 150);
        }
    }

    private void createColorPanel() {
        colorPanel = new JPanel(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        // creates the three textfields to input rgb values.
        // the first is r, second, b third g
        redField = new JTextField(2);
        c.fill = GridBagConstraints.VERTICAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(10, 0, 0, 10);
        colorPanel.add(redField, c);

        greenField = new JTextField(2);
        c.fill = GridBagConstraints.VERTICAL;
        c.weightx = 0.0;
        c.gridx = 1;
        c.gridy = 1;
        colorPanel.add(greenField, c);

        blueField = new JTextField(2);
        c.fill = GridBagConstraints.VERTICAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 2;
        colorPanel.add(blueField, c);

        redField.addActionListener(this);
        greenField.addActionListener(this);
        blueField.addActionListener(this);

        this.add(colorPanel, BorderLayout.EAST);
    }

    public void actionPerformed(ActionEvent arg0) {
        redInt = Integer.parseInt(redField.getText());
        greenInt = Integer.parseInt(greenField.getText());
        blueInt = Integer.parseInt(blueField.getText());

        colorPanel.add(color);

    }

    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ColorFrame frame = new ColorFrame(200, 200);
            }
        });
    }
}

Another Option is just use a regular JPanel 另一种选择就是使用常规的JPanel

private JPanel createCPanel() {
    return new JPanel() {
        {
            setBackground(Color.BLUE);
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(150, 150);
        }
    };
}

and just call setBackground(Color) on the panel. 然后在面板上调用setBackground(Color) You need to keep in mind the above changes also though. 不过,您还需要记住上述变化。

而不是创建颜色图标尝试使用背景颜色设置您想要的,然后设置标签的大小。

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

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