简体   繁体   English

Jlabel在鼠标单击Java时更改颜色

[英]Jlabel change color on mouse click Java

I am adding multiple Jlabel using for loop. 我正在使用for循环添加多个Jlabel。 What I want is on mouse click, the color of the selected JLabel should change and set to the default color on click of another JLabel . 我想要的是单击鼠标,所选JLabel的颜色应更改,并在单击另一个JLabel设置为默认颜色。

在这里输入代码

Since you changed the state of the label, you need someway to change it back. 由于您更改了标签的状态,因此需要某种方式将其改回。 The simplest solution is to maintain a reference to the last label that was changed and when the new label is clicked, reset it's state 最简单的解决方案是维护对最后更改的标签的引用,并在单击新标签时重置其状态。

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            HighlightMouseListener hml = new HighlightMouseListener();
            for (int index = 0; index < 10; index++) {
                JLabel label = new JLabel("Hello " + index);
                label.addMouseListener(hml);
                add(label, gbc);
            }
        }

    }

    public class HighlightMouseListener extends MouseAdapter {
        private JLabel previous;

        @Override
        public void mouseClicked(MouseEvent e) {
            Component source = e.getComponent();
            if (!(source instanceof JLabel)) {
                return;
            }
            JLabel label = (JLabel) source;
            if (previous != null) {
                previous.setBackground(null);
                previous.setForeground(null);
                previous.setOpaque(false);
            }
            previous = label;
            label.setForeground(Color.WHITE);
            label.setBackground(Color.BLUE);
            label.setOpaque(true);
        }

    }

}

I still wonder if a JList would be a better and simpler solution, but since I don't know what you're doing, it's all I can do 我仍然想知道JList是否会是更好,更简单的解决方案,但是由于我不知道您在做什么,这就是我所能做的

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

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