简体   繁体   English

如何通过鼠标事件更改JLabel的背景颜色?

[英]How to change JLabel's background color through mouse events?

I have aa JPanel with 9 JLabels with all of them setBackground() to the color of white and I have the mouse listener interface implemented in the class. 我有一个带有9个JLabels JPanel ,所有这些都是setBackground()到白色的颜色,我在类中实现了鼠标监听器接口。 My purpose is when i left click on my mouse in the region of a JLabel it will change the color to black or vice versa. 我的目的是当我在JLabel区域中单击我的鼠标时,它会将颜色更改为黑色,反之亦然。 This is my first time working with mouse events and I'm stuck. 这是我第一次使用鼠标事件而且我被卡住了。 I don't seem to get the whole concept right. 我似乎没有把整个概念说得对。 Can you guys help me out here? 你能帮助我吗? Thanks in advance! 提前致谢! Oh by the way, I do understand that if I were to use a for-loop the process will be so much easier but I just want to find out whether we can do it the harder way. 哦顺便说一句,我明白如果我要使用for-loop ,那么这个过程会更容易,但我只是想知道我们是否可以用更难的方式去做。 Cheers! 干杯!

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

class TilePanelA extends JPanel implements MouseListener
{
JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JLabel label5;
JLabel label6;
JLabel label7;
JLabel label8;
JLabel label9;


public TilePanelA() 
{
    this.setLayout(new GridLayout(3, 3));
    Dimension labelSize = new Dimension(300, 300);

    label1 = new JLabel();
    label2 = new JLabel();
    label3 = new JLabel();
    label4 = new JLabel();
    label5 = new JLabel();
    label6 = new JLabel();
    label7 = new JLabel();
    label8 = new JLabel();
    label9 = new JLabel();

    label1.setPreferredSize(labelSize);
    label2.setPreferredSize(labelSize);
    label3.setPreferredSize(labelSize);
    label4.setPreferredSize(labelSize);
    label5.setPreferredSize(labelSize);
    label6.setPreferredSize(labelSize);
    label7.setPreferredSize(labelSize);
    label8.setPreferredSize(labelSize);
    label9.setPreferredSize(labelSize);

    label1.setBackground(Color.WHITE);
    label2.setBackground(Color.WHITE);
    label3.setBackground(Color.WHITE);
    label4.setBackground(Color.WHITE);
    label5.setBackground(Color.WHITE);
    label6.setBackground(Color.WHITE);
    label7.setBackground(Color.WHITE);
    label8.setBackground(Color.WHITE);
    label9.setBackground(Color.WHITE);

    label1.setOpaque(true);
    label2.setOpaque(true);
    label3.setOpaque(true);
    label4.setOpaque(true);
    label5.setOpaque(true);
    label6.setOpaque(true);
    label7.setOpaque(true);
    label8.setOpaque(true);
    label9.setOpaque(true);

    this.add(label1);
    this.add(label2);
    this.add(label3);
    this.add(label4);
    this.add(label5);
    this.add(label6);
    this.add(label7);
    this.add(label8);
    this.add(label9);

    label1.addMouseListener(this);
    label2.addMouseListener(this);
    label3.addMouseListener(this);
    label4.addMouseListener(this);
    label5.addMouseListener(this);
    label6.addMouseListener(this);
    label7.addMouseListener(this);
    label8.addMouseListener(this);
    label9.addMouseListener(this);  
} // end of constructor

public void mousePressed(MouseEvent e)
{
    if (e.getSource() == Color.WHITE)
    {
        setBackground(Color.BLACK);
    } else
    {
        setBackground(Color.WHITE);
    }
}

public void mouseReleased(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{
}
}

e.getSource() will return the label that is clicked, not the color of that label (in your example, anyway) e.getSource()将返回单击的标签,而不是该标签的颜色(在您的示例中,无论如何)

So, cast to a label: 所以,投下一个标签:

JLabel theLabel = (JLabel) e.getSource();

then setBackground on it appropriately: 适当的setBackground:

if (theLabel.getBackground().equals(Color.WHITE)) theLabel.setBackgound(...);

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

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