简体   繁体   English

Java swing-使用按钮更改包含图标的标签的颜色

[英]Java swing - Change color of a label containing icon using buttons

I am trying to change the color of an icon using 3 different JButtons, each for a different color. 我试图使用3个不同的JButton更改图标的颜色,每个JButton的颜色不同。

The icon is a circle that starts out as red default. 该图标是一个以红色默认值开头的圆圈。 When the user clicks the button called "blue" the circle changes color to blue, same idea for "green". 当用户单击名为“蓝色”的按钮时,圆圈将颜色更改为蓝色,与“绿色”相同。

To update this icon color, i am supposed to use the repaint() method. 要更新此图标颜色,我应该使用repaint()方法。

Here is my method to create the red circle icon. 这是我创建红色圆圈图标的方法。

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;


public class ColorIcon implements Icon
{
private int width;
private Color color;
private ChangeColor c;


public ColorIcon(int aWidth, ChangeColor c)
{
this.c = c;
width = aWidth;
color = Color.RED;
}

public int getIconWidth()
{
return width;
}

public int getIconHeight()
{
return width / 2;
}

public void setColor(Color c)
{
color = c;
}

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

color = c.color;
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double ellipse = new Ellipse2D.Double(x,y, width, width);
g2.setColor( color );
g2.fill( ellipse );
}
}

And here is the class i use to test it. 这是我用来测试它的课程。 This class is the one with the event handler that creates the buttons that when pressed should change the color of the icon. 此类是带有事件处理程序的类,该事件处理程序创建按钮,当按下按钮时,按钮应会更改图标的颜色。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestColorIcon{

public java.awt.Color color = java.awt.Color.RED;

public Component createComponents()
{ 
JButton buttonRed = new JButton("Red");
JButton buttonBlue = new JButton("Blue");
JButton buttonGreen = new JButton("Green");

final ColorIcon icon = new ColorIcon( 20);
final JLabel label = new JLabel( icon );

JPanel panel = new JPanel();
panel.setLayout( new GridLayout(0, 3) );
panel.add( buttonRed );
panel.add( buttonBlue );
panel.add( buttonGreen );
panel.add( label );

buttonRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
color = Color.RED;
label.repaint();
}
});

buttonBlue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
color = Color.BLUE;
label.repaint();
}
});

buttonGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
color = Color.GREEN;
label.repaint();
}
});

return panel;
}


public static void main(String[] args)
{    

JFrame frame = new JFrame();
ChangeColor changeColor = new ChangeColor(); 
Component content = changeColor.createComponents();
frame.getContentPane().add( content );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}

When I run this, i get an error saying compiler cannot resolve class ColorIcon. 当我运行此命令时,出现错误消息,提示编译器无法解析类ColorIcon。 I am not sure how to fix this error and get the desired output. 我不确定如何解决此错误并获得所需的输出。

The code has gotten a bit long so i am unable to pinpoint the exact reason for the error, so any help is appreciated. 该代码已经有点长,所以我无法查明错误的确切原因,因此,我们将不胜感激。

You never seem to change the color of the ColorIcon itself 您似乎从未改变过ColorIcon本身的颜色

buttonRed.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        color = Color.RED;
        label.repaint();
    }
});

Instead, make sure you are applying the color to the icon as well. 相反,请确保同时将颜色应用于图标。

buttonRed.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        color = Color.RED;
        icon.setColor(color);
        label.repaint();
    }
});

Icon should not be relying on any other information other then what it's provide for itself, so instead of trying to get the color from yet another source, simply use the support that the ColorIcon already has Icon不应该依赖于其自身提供的信息以外的其他任何信息,因此与其尝试从其他来源获取颜色, ColorIcon使用ColorIcon已经拥有的支持

When I run this, i get an error saying compiler cannot resolve class ColorIcon. 当我运行此命令时,出现错误消息,提示编译器无法解析类ColorIcon。

Your constructor of the ColorIcon class is 您的ColorIcon类的构造函数是

public ColorIcon(int aWidth, ChangeColor c)

It expects one int and one ChnageColor arguments. 它需要一个int和一个ChnageColor参数。

And you are calling it with wrong parameters. 而且您用错误的参数来调用它。

final ColorIcon icon = new ColorIcon(20);

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

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