gpt4 book ai didi

java - 更改 JComponent 的选项卡遍历顺序?

转载 作者:行者123 更新时间:2023-11-30 03:46:23 24 4
gpt4 key购买 nike

我有一个由许多小组件组成的大组件,那么将使用什么来确定这些小组件的 Tab 键顺序?

还有,有没有办法设置FocusTraversalPolicy对于JComponent

最佳答案

also, is there a way to set FocusTraversalPolicy for JComponent?

是的。看看How to Use the Focus Subsystem 。有一个部分 Customizing Focus Traversal这通过一个完整的示例 FocusTraversalDemo 准确地解释了如何做到这一点。它基本上创建了一个扩展 FocusTraversalPolicy 的类其中它重写了一些方法来确定组件遍历的顺序

<小时/>

更新

这是一个使用 RandomFocusTraversalPolicy 的示例,正​​如其名称所示。遍历是随机的。

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class FocusTraversalDemo extends JComponent {

List<Component> components = new ArrayList<Component>();

public FocusTraversalDemo() {
setFocusTraversalPolicyProvider(true);
setLayout(new GridLayout(5, 5, 10, 10));
initComponents();
setFocusTraversalPolicy(new RandomFocusTraversalPolicy(components));
}

private void initComponents() {
for (int i = 0; i < 25; i++) {
final JButton button = new JButton("Button " + i);
final Color defaultColor = button.getForeground();
button.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent e) {
button.setForeground(Color.BLUE);
}
@Override
public void focusLost(FocusEvent e) {
button.setForeground(defaultColor);
}
});
this.add(button);
components.add(button);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JFrame frame = new JFrame();
frame.add(new FocusTraversalDemo());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class RandomFocusTraversalPolicy extends FocusTraversalPolicy {

private List<Component> comps;

public RandomFocusTraversalPolicy(List<Component> comps) {
this.comps = comps;
}

@Override
public Component getComponentAfter(Container aContainer, Component aComponent) {
return comps.get(getRandomIndex());

}

@Override
public Component getComponentBefore(Container aContainer, Component aComponent) {
return comps.get(getRandomIndex());
}

@Override
public Component getFirstComponent(Container aContainer) {
return comps.get(0);
}

@Override
public Component getLastComponent(Container aContainer) {
return comps.get(comps.size() - 1);
}

@Override
public Component getDefaultComponent(Container aContainer) {
return comps.get(0);
}

private int getRandomIndex() {
Random random = new Random();
return random.nextInt(comps.size());
}
}
}

关于java - 更改 JComponent 的选项卡遍历顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25576647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com