鼠标专注于JLabel

时间:2013-06-20 19:26:25

标签: java swing focus jlabel mouselistener

我想知道是否有任何方法可以将注意力集中在我的JLabel上。 我有一系列标签,我想知道我是否选择了其中任何一个。

我想使用MouseListener但不知道JLabel的哪个属性用于设置焦点或在某种模式下说我选择的是该标签。

谢谢大家,抱歉我的英语不好。

2 个答案:

答案 0 :(得分:0)

要检测标签上是否有人点击,此代码将有效:

package com.sandbox;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SwingSandbox {

    public static void main(String[] args) {
        JFrame frame = buildFrame();

        JPanel panel = new JPanel();
        BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
        panel.setLayout(layout);
        frame.getContentPane().add(panel);

        JLabel label = new JLabel("Click me and I'll react");
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("clicked!");
            }
        });
        panel.add(label);
        panel.add(new JLabel("This label won't react"));
    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}

答案 1 :(得分:0)

您可以向FocusListener添加JLabel,以便每当收到焦点时,都会通知其听众。这是一个示例测试。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class JLabelFocusTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame("Demo");

                frame.getContentPane().setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JLabel lblToFocus = new JLabel("A FocusEvent will be fired if I got a focus.");
                JButton btnFocus = new JButton("Focus that label now!");

                btnFocus.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        lblToFocus.requestFocusInWindow();
                    }
                });

                lblToFocus.addFocusListener(new FocusAdapter() {
                    @Override
                    public void focusGained(FocusEvent e) {
                        super.focusGained(e);

                        System.out.println("Label got the focus!");
                    }
                });

                frame.getContentPane().add(lblToFocus, BorderLayout.PAGE_START);
                frame.getContentPane().add(btnFocus, BorderLayout.CENTER);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}