更改JLabel中图标的颜色

时间:2019-11-10 08:13:44

标签: java swing

我需要制作一个程序,该程序在jframe中显示三个按钮以及一个最初的红色圆圈。这三个按钮必须分别显示“红色”,“绿色”和“蓝色”,并且当您单击按钮时,红色圆圈应该变为您单击的任何颜色。

起初,我实际上尝试更改图标的颜色,但是我认为将三个圆圈分别设置为不同的颜色并向每个按钮添加一个动作侦听器会更容易,这将使正确的圆圈变成框架同时在用户单击颜色时替换上一个颜色。我在弄清楚该怎么做时遇到麻烦。我应该为每个圈子设置三个单独的班级吗?还是有更简单的方法?

另一件事是,我必须使用JLabel,以便可以在项目的每个颜色更改的末尾调用repaint()方法。我还需要在main方法中添加一个静态方法,该方法返回一个动作侦听器,但我还没有弄清楚该怎么做。

这是我到目前为止所拥有的:

/**
 * Write a description of class CircleIcon here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class CircleIcon implements Icon
{
    // Instance variables - replace the example below with your own

    /**
     * Constructor for objects of class CircleIcon
     */
    private int size;

    public CircleIcon(int aSize)
    {
        // Initialise instance variables
        size = aSize;
    }

    public int getIconWidth() {
        return size;
    }

    public int getIconHeight() {
        return size;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2 = (Graphics2D) g;
        Ellipse2D.Double circle = new Ellipse2D.Double(x, y, size, size);
        g2.setColor(Color.RED);
        g2.fill(circle);
    }
}

CircleIconTester类:

/**
 * Write a description of class CircleIconTester here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.color.*;

public class CircleIconTester
{
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        CircleIcon circle = new CircleIcon(50);
        JLabel label = new JLabel(circle);
        frame.add(label);

        JButton red = new JButton("RED");
        JButton blue = new JButton("BLUE");
        JButton green = new JButton("GREEN");
        frame.add(red);
        frame.add(blue);
        frame.add(green);

        ActionListener redAL = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            }
        };

        red.addActionListener(redAL);

        frame.setLayout(new FlowLayout());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

 }

3 个答案:

答案 0 :(得分:2)

更改JLabel的(前景)颜色是对setForeground(...)对象的JLabel方法的简单调用。但是您的图标实现必须获取其所放置组件的属性。幸运的是,paintIcon()方法将返回图标所放置的父组件。请参见documentation of paintIcon()

void paintIcon(Component c,
               Graphics g,
               int x,
               int y)
     

在指定位置绘制图标。图标的实现可以使用Component参数来获取对绘画有用的属性,例如前景或背景色。

文档甚至提到您可以使用它来获取颜色。 在您的paintIcon()方法内,您可以使用getForeground()方法来获取JLabel的前景色。

public void paintIcon(Component c, Graphics g, int x, int y){
    Graphics2D g2 = (Graphics2D) g;
    Ellipse2D.Double circle = new Ellipse2D.Double(x, y, size, size);
    g2.setColor(c.getForeground()); // <-- get foreground color from parent.
    g2.fill(circle);
}

现在,您必须在动作侦听器中设置正确的前景色。当您要使用静态方法构建动作侦听器时,可以执行此操作。创建一个新的静态方法BuildActionListener,该方法将获得两个参数。一个用于更改JLabel对象,另一个用于使用前景色。它返回一个ActionListener对象,该对象会更改前景色:

/**
 * Build an action listener to change the color of the label.
 *
 * @param label The label to change.
 * @param color The color to use.
 * @returns The action listener which changes the color.
 */
public static ActionListener BuildActionListener(JLabel label, Color color) {
    return new ActionListener(){
        public void actionPerformed(ActionEvent event){
            label.setForeground(color);
        }
    };
}

使用此帮助程序方法为每个按钮分配自定义操作侦听器:

red.addActionListener(BuildActionListener(label, Color.RED));
blue.addActionListener(BuildActionListener(label, Color.BLUE));
green.addActionListener(BuildActionListener(label, Color.GREEN));

要以红色圆圈(而不是黑色圆圈)开头,请将标签的前景色设置在开头的某个位置:

JLabel label = new JLabel(circle);
label.setForeground(Color.RED);

答案 1 :(得分:0)

简单的代码是:

label.setForeground(Color.red);

答案 2 :(得分:0)

尝试以下方法。它的工作原理:

red.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        label.setForeground(Color.red);
    }
});