我有問題更改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