SwingJava是一种无需鼠标侦听器就可以请求关注单击的简单方法吗? 1 年,6 月 Questions & Answers 985 对于JComponent来说,当您单击它时,有没有比设置鼠标侦听器更简单的方法来请求焦点
# 1 楼答案 您需要向组件添加一个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()); } }
# 2 楼答案 您可以从类JLabel继承一个名为JFocusedLabel的类,该类将有一个MouseListener来进行聚焦。您可能需要实现5-6个这样的类,但最终您不必将这样的侦听器添加到您拥有的每个JComponent对象中。只是实例化那些类,而不是它们的标准父类
# 1 楼答案
您需要向组件添加一个MouseListener,以便它能够响应鼠标按下并请求应用程序焦点。对多个组件执行此操作的最简单方法是使用循环,例如在组件创建时使用的for循环,或者将组件放置在容器(例如ArrayList)中,并通过添加MouseListener(MouseAdapter)循环此集合。例如:
可以这样使用:
# 2 楼答案
您可以从类
JLabel
继承一个名为JFocusedLabel
的类,该类将有一个MouseListener
来进行聚焦。您可能需要实现5-6个这样的类,但最终您不必将这样的侦听器添加到您拥有的每个JComponent
对象中。只是实例化那些类,而不是它们的标准父类