我已经创建了许多文本字段,我想知道如何通过按回车键在字段之间切换焦点。另外,我可以控制目标区域吗?例如,我可以通过按 Enter 在 field A 重点将改为 field C ?
Enter
field A
field C
dphi5xsq1#
看看如何使用焦点子系统它将解释如何为焦点子系统设置转移键简单的例子
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); } } }
af7jpaap2#
简单示例:假设你有两个 JTextField 学生: textField 以及 textField1 ```textField.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {textField1.requestFocusInWindow(); }});
JTextField
textField
textField1
textField1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {textField.requestFocusInWindow();}});
现在当你击中 `Enter` 当专注于第一个 `JTextField` ,另一个会聚焦,反之亦然。
2条答案
按热度按时间dphi5xsq1#
看看如何使用焦点子系统
它将解释如何为焦点子系统设置转移键
简单的例子
af7jpaap2#
简单示例:
假设你有两个
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();
}
});