我有一个面板null布局,并有以下代码
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键时,光标将移动到下一个水平Textfield。如何使其移动或指向下一个水平Textfield
最诚挚的问候, Wesam
答案 0 :(得分:0)
这个问题似乎与Press TAB to next Java in Vertical axis component重复。
看起来重复你的问题可能是有益的... ;-)为方便起见,我也在这里重复了我的答案 - 再次使用如何使用焦点子系统中的Customizing Focus Traversal主题< / em>已经提到的教程(在你的另一个问题的答案中)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);
}
}