Java - 在没有鼠标侦听器的情况下请求关注点击的简单方法?
Java - An easy way to request focus on click without a mouse listener?
有没有比必须设置 MouseListener 更简单的方法让 JComponent 在您单击它时请求焦点?
你可以从 class 继承,比如 JLabel
一个叫做 JFocusedLabel
的 class,它会有一个 MouseListener
,它会做聚焦.您可能需要实现 5-6 个这样的 classes,但最终您不必为每个 JComponent
对象都添加这样的侦听器。只需实例化那些 classes 而不是它们的标准父代。
您需要向组件添加 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());
}
}
有没有比必须设置 MouseListener 更简单的方法让 JComponent 在您单击它时请求焦点?
你可以从 class 继承,比如 JLabel
一个叫做 JFocusedLabel
的 class,它会有一个 MouseListener
,它会做聚焦.您可能需要实现 5-6 个这样的 classes,但最终您不必为每个 JComponent
对象都添加这样的侦听器。只需实例化那些 classes 而不是它们的标准父代。
您需要向组件添加 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());
}
}