Java - 在没有鼠标监听器的情况下请求关注点击的简单方法?

时间:2017-11-11 18:05:13

标签: java swing focus jcomponent

点击它时,JComponent是否有更简单的方法来请求焦点而不是设置MouseListener?

2 个答案:

答案 0 :(得分:2)

您需要将MouseListener添加到组件,以便它能够响应鼠标按下并请求应用程序焦点。对多个组件执行此操作的最简单方法是使用循环,例如在组件创建时使用的for循环,或者将组件放在容器(如ArrayList)中,并循环遍历此集合,添加MouseListener(MouseAdapter)。例如:

private class MyMouse extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent e) {
        JComponent comp = (JComponent) e.getSource();
        comp.requestFocusInWindow();
    }
}

可以这样使用:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class FocusTest extends JPanel {

    private static final int ROWS = 6;
    private static final int COLS = 10;
    protected static final int THICKNESS = 5;
    private static final int PREF_W = 500;
    private static final int PREF_H = 300;
    private static final Color SELECTED_COLOR = Color.RED;
    private static final Color UNSELECTED_COLOR = Color.LIGHT_GRAY;

    public FocusTest() {
        setLayout(new GridLayout(ROWS, COLS, 2, 2));
        MyMouse myMouse = new MyMouse();
        MyFocus myFocus = new MyFocus();
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                String text = String.format("[%d, %d]", j, i);
                final JLabel label = new JLabel(text, SwingConstants.CENTER);
                label.setBorder(createBorder(false));
                label.addFocusListener(myFocus);
                label.addMouseListener(myMouse);
                label.setFocusable(true);
                add(label);
            }
        }
    }

    public Border createBorder(boolean selected) {
        Color color = selected ? SELECTED_COLOR : UNSELECTED_COLOR;
        return BorderFactory.createLineBorder(color, THICKNESS);
    }

    private class MyMouse extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            comp.requestFocusInWindow();
        }
    }

    private class MyFocus extends FocusAdapter {

        @Override
        public void focusGained(FocusEvent e) {
            JComponent comp = (JComponent) e.getSource();
            comp.setBorder(createBorder(true));
        }

        @Override
        public void focusLost(FocusEvent e) {
            JComponent comp = (JComponent) e.getSource();
            comp.setBorder(createBorder(false));
        }
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension superSize = super.getPreferredSize();
        if (isPreferredSizeSet()) {
            return superSize;
        }
        int w = Math.max(PREF_W, superSize.width);
        int h = Math.max(PREF_H, superSize.height);
        return new Dimension(w, h);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("FocusTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new FocusTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

答案 1 :(得分:0)

你可以从一个类继承,比如JLabel一个名为JFocusedLabel的类,它会有一个MouseListener,可以进行聚焦。您可能需要实现5-6个这样的类,但在一天结束时,您不必为每个JComponent对象添加这样的侦听器。只是实例化那些类而不是他们的标准父母。