0

我想知道是否有任何方法可以将焦点放在我的JLabel. 我有一系列标签,我想知道我是否选择了其中任何一个。

我想使用 aMouseListener但不知道使用什么属性JLabel来设置焦点或在某种模式下说我正在选择该标签。

谢谢大家,对不起我的英语不好。

4

2 回答 2

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;
    }


}
于 2013-06-20T20:25:31.920 回答
0

您可以将 a 添加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);
            }
        });
    }
}
于 2013-06-21T01:33:34.550 回答