六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

Swing中设置按Tab键后焦点的顺序

[复制链接]

升级  39.33%

29

主题

29

主题

29

主题

秀才

Rank: 2

积分
109
 楼主| 发表于 2013-1-27 05:20:18 | 显示全部楼层 |阅读模式
Swing中设置按Tab键后焦点的顺序
 
import javax.swing.*;import java.awt.*;import java.util.Arrays;import java.util.ArrayList;/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */public class BorderFocus {  public static void main(String args[]) {    JFrame frame = new JFrame("Focus Cycling");    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    Container contentPane = frame.getContentPane();    JButton north = new JButton("North");    contentPane.add(north, BorderLayout.NORTH);    JButton south = new JButton("South");    contentPane.add(south, BorderLayout.SOUTH);    JButton east = new JButton("East");    contentPane.add(east, BorderLayout.EAST);    JButton west = new JButton("West");    contentPane.add(west, BorderLayout.WEST);    JButton center = new JButton("Center");    contentPane.add(center, BorderLayout.CENTER);    contentPane.setFocusable(false);     ArrayList<Component> list = new ArrayList<Component>();          list.add(north);     list.add(east);     list.add(south);     list.add(west);     list.add(center);     final ArrayList comList = list;    FocusTraversalPolicy policy = new FocusTraversalPolicy() {      public Component getFirstComponent(Container focusCycleRoot) {        return  (Component)comList.get(0);      }      public Component getLastComponent(Container focusCycleRoot) {        return (Component) comList.get(comList.size()-1);      }      public Component getComponentAfter(Container focusCycleRoot,           Component aComponent) {        int index = comList.indexOf(aComponent);                return (Component) comList.get((index + 1) % comList.size());      }      public Component getComponentBefore(Container focusCycleRoot,           Component aComponent) {        int index = comList.indexOf(aComponent);        return (Component) comList.get((index - 1 + comList.size()) % comList.size());      }      public Component getDefaultComponent(Container focusCycleRoot) {        return (Component) comList.get(0);      }    };    frame.setFocusTraversalPolicy(policy);    frame.pack();    frame.show();   }} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表