我创建了多个文本字段,我想知道如何通过按Enter键在字段之间切换焦点。
此外,我可以控制目标字段吗?例如,我可以通过在Enter
中按field A
来定义焦点将更改为field C
吗?
答案 0 :(得分:6)
简单示例:
假设您有两个JTextField
:textField
和textField1
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.requestFocusInWindow();
}
});
textField1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.requestFocusInWindow();
}
});
现在当你专注于第一个Enter
时点击JTextField
,另一个会聚焦,反之亦然。
答案 1 :(得分:5)
查看How to Use the Focus Subsystem
它将说明如何设置焦点子系统的传输键
简单示例
public class TestFocusTransfersal {
public static void main(String[] args) {
new TestFocusTransfersal();
}
public TestFocusTransfersal() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField field1 = new JTextField("1", 10);
private JTextField field2 = new JTextField("2", 10);
private JTextField field3 = new JTextField("3", 10);
private JTextField field4 = new JTextField("4", 10);
private JTextField field5 = new JTextField("5", 10);
private JTextField field6 = new JTextField("6", 10);
private JTextField field7 = new JTextField("7", 10);
private JTextField field8 = new JTextField("8", 10);
private JTextField field9 = new JTextField("9", 10);
private final MyOwnFocusTraversalPolicy policy;
public TestPane() {
// Set up enter for focus transfer...
Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
newForwardKeys);
setLayout(new GridBagLayout());
add("Field #1: ", field1, 0, 0);
add("Field #2: ", field2, 2, 0);
add("Field #3: ", field3, 4, 0);
add("Field #4: ", field4, 6, 0);
add("Field #5: ", field5, 8, 0);
add("Field #6: ", field6, 2, 1);
add("Field #7: ", field7, 4, 1);
add("Field #8: ", field8, 6, 1);
add("Field #9: ", field9, 8, 1);
policy = new MyOwnFocusTraversalPolicy(
field1,
field6,
field7,
field8,
field9,
field2,
field3,
field4,
field5
);
// You can do this to make life easier, but it may have unexpected
// consequences...
// setFocusCycleRoot(true);
// setFocusTraversalPolicy(policy);
}
@Override
public void addNotify() {
super.addNotify();
// Comment this out if you use focusCycleRoot
SwingUtilities.getWindowAncestor(this).setFocusTraversalPolicy(policy);
}
protected void add(String label, JTextField field, int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = y;
gbc.gridx = x;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.anchor = GridBagConstraints.EAST;
add(new JLabel(label), gbc);
gbc.gridx++;
add(field, gbc);
}
}
public static class MyOwnFocusTraversalPolicy
extends FocusTraversalPolicy {
private List<Component> order;
public MyOwnFocusTraversalPolicy(Component... order) {
this.order = new ArrayList<>(Arrays.asList(order));
}
@Override
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
int idx = (order.indexOf(aComponent) + 1) % order.size();
return order.get(idx);
}
@Override
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
return order.get(idx);
}
@Override
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
@Override
public Component getLastComponent(Container focusCycleRoot) {
return order.size() > 0 ? order.get(order.size()) : null;
}
@Override
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(0);
}
}
}