java - 更改 JLabel 中图标的颜色
问题描述
我需要制作一个程序,显示三个按钮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);
}
}
解决方案
更改 a 的(前景)颜色是对对象的方法JLabel
的简单调用。但是您的图标实现必须获取它所在组件的属性。幸运的是,该方法返回了图标所在的父组件。请参阅以下文档:setForeground(...)
JLabel
paintIcon()
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);
推荐阅读
- java - 无法插入到 SQLite 数据库
- c++ - NS3-如何在模拟运行期间修改点对点通道数据速率
- html - 边距不当
- android - 如何在 android pie 或更高版本中运行后台操作?
- spring - 使用 JWT 调用资源服务器通过 Spring Cloud Gateway 返回 HTTP 403
- haskell - Haskell 回溯
- python - 如何扫描类的实例以返回具有最高特定属性值 PYTHON 的实例名称
- pygame - 当我尝试在目标上调用方法时,为什么会出现 Pygame 属性错误?
- html - 为什么网格单元格只包含 div 的第一个元素?
- ios - 动态添加跨越 n 个表格视图单元格的形状