我有一个面板空布局,并有以下代码
int k=130;
int h=10;
for (int i=0; i<22; ++i) {
jTextFieldArray[i] = new JTextField();
jTextFieldArray[i].setBounds(k, h, 120, 25);
String s = Integer.toString(i+1);
jTextFieldArray[i].setText(s);
h+=30;
panel.add(jTextFieldArray[i]);
if (i==10) k=430;
if (i==10) h=10;
}
按TAB键时,光标将移动到下一个水平文本字段。如何移动或指向下一个水平文本字段
谨致问候,Wesam
这个问题似乎是垂直轴组件中按TAB键到下一个Java的重复。
看起来重复你的问题是有益的…;-)为了方便起见,我在这里也重复了我的答案——再次使用Aqua:已经提到的如何使用焦点子系统教程中的自定义焦点遍历主题:
private void tryCustomFocusTraversal() {
final JFrame frame = new JFrame("Stack Overflow: vertical tab order");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(null);
final JTextField[] jTextFieldArray = new JTextField[22];
int k = 130;
int h = 10;
for (int i = 0; i < jTextFieldArray.length; ++i) {
jTextFieldArray[i] = new JTextField();
jTextFieldArray[i].setBounds(k, h, 120, 25);
String s = Integer.toString(i + 1);
jTextFieldArray[i].setText(s);
h += 30;
panel.add(jTextFieldArray[i]);
if (i == 10) k = 430;
if (i == 10) h = 10;
}
frame.getContentPane().add(panel);
frame.setFocusTraversalPolicy(new CustomFocusTraversalPolicy(Arrays.asList(jTextFieldArray)));
frame.setVisible(true);
}
它使用CustomFocusTraversalPolicy
类:
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.util.ArrayList;
import java.util.List;
public class CustomFocusTraversalPolicy extends FocusTraversalPolicy {
private final List<Component> componentOrder = new ArrayList<>();
public CustomFocusTraversalPolicy(final List<Component> componentOrder) {
this.componentOrder.addAll(componentOrder);
}
public Component getComponentAfter(final Container focusCycleRoot, final Component aComponent) {
return componentOrder.get((componentOrder.indexOf(aComponent) + 1) % componentOrder.size());
}
public Component getComponentBefore(final Container focusCycleRoot, final Component aComponent) {
final int currentIndex = componentOrder.indexOf(aComponent);
return componentOrder.get(currentIndex > 0 ? currentIndex - 1 : componentOrder.size() - 1);
}
public Component getFirstComponent(final Container focusCycleRoot) {
return componentOrder.get(0);
}
public Component getLastComponent(final Container focusCycleRoot) {
return componentOrder.get(componentOrder.size() - 1);
}
public Component getDefaultComponent(final Container focusCycleRoot) {
return getFirstComponent(focusCycleRoot);
}
}