我有问题更改JLabel颜色。我正在使用三个JLabel
变量。我在这个JLabel
变量上放置鼠标事件。我运行,并且当我在JLabels
上委托鼠标时,两者都变色。我whis is的是,当我在JLabel
变量上输入鼠标时,其中一个JLabel
改变了颜色。如何更改JLabel颜色
请解决此问题。
我有问题更改JLabel颜色。我正在使用三个JLabel
变量。我在这个JLabel
变量上放置鼠标事件。我运行,并且当我在JLabels
上委托鼠标时,两者都变色。我whis is的是,当我在JLabel
变量上输入鼠标时,其中一个JLabel
改变了颜色。如何更改JLabel颜色
请解决此问题。
不能完全确定你在问什么......我认为你的问题是,你有标签和时你将鼠标放入其中一个你想要的即标签有红色背景,而不是两个。
为此,您可以使用e.getComponent()
来获取触发鼠标事件的标签,然后仅为该标签设置背景。此外,您可能希望使用setBackground(null)
来重置背景颜色,因为底层框架的背景可能不总是白色。最后,您可以使用MouseAdapter
类而不是MouseListener
,为所有其他您不需要的方法提供默认值(no-op)。
MouseListener ma = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
e.getComponent().setBackground(Color.RED);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(null);
}
};
你的问题是方法setBackground()
,更改setForeground()
:
entry.addMouseListener(this);
entry.setOpaque(true);
profile.addMouseListener(this);
profile.setOpaque(true);
public void mouseClicked(MouseEvent mc)
{}
public void mouseEntered(MouseEvent me)
{
entry.setForeground(Color.red);
profile.setForeground(Color.red);
}
public void mouseExited(MouseEvent me)
{
entry.setForeground(Color.white);
profile.setForeground(Color.white);
}
public void mousePressed(MouseEvent mp)
{}
public void mouseReleased(MouseEvent mr)
{}
你的问题是什么?我不明白.Btw不是setForeground(颜色)你想要什么?改变标签的字体颜色。 –
您是否问为什么_both_标签在您将鼠标移动到_one_上时会改变颜色?在这种情况下,请尝试'me.getSource()。setBackground(...)'。 –
@tobias_k:最好'getComponent()'而不是'getSource()',否则你需要一个类型转换。但我想你已经明白了。 – Holger