光标自动走位问题

lightyear307 2008-04-28 01:51:58
我现在在一个版面上有4个JTextField,现在我想让界面在跳出的时候就让光标停在第三个文本框里,请问应该怎么做呢
...全文
110 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
LexChen 2008-04-29
  • 打赏
  • 举报
回复
试试 txt.setSelectedStart(0); txt.setSelectedEnd(txt.getText().length());
lightyear307 2008-04-29
  • 打赏
  • 举报
回复
楼上的,这个方法似乎不可行啊
Inhibitory 2008-04-28
  • 打赏
  • 举报
回复
public void selectText(int startIndex,
int endIndex)
Selects the text between two indices.

Specified by:
selectText in interface AccessibleEditableText
Parameters:
startIndex - the starting index in the text
endIndex - the ending index in the text
Since:
1.4
lightyear307 2008-04-28
  • 打赏
  • 举报
回复
谢谢大家如此踊跃啊!!
我现在碰到了一个新问题,其实我这个问题是在判断了所有文本框中的内容后再实现光标走位的,上面几位说得都是对了,我也实现了这个功能,但是,我在 判断完后,输入界面回停留在当前,却不是选中状态,我想知道,如何让此时的界面处于选中状态
cuijie_cn 2008-04-28
  • 打赏
  • 举报
回复
学习一下
Inhibitory 2008-04-28
  • 打赏
  • 举报
回复
写了个小例子, 用FocusListener还不行, 换成了WindowListener可以:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test01 extends JFrame {
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;

public Test01() {
tf1 = new JTextField(20);
tf2 = new JTextField(20);
tf3 = new JTextField(20);
tf4 = new JTextField(20);

Box vBox = Box.createVerticalBox();
vBox.add(tf1);
vBox.add(tf2);
vBox.add(tf3);
vBox.add(tf4);

this.getContentPane().add(vBox);

this.addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
tf3.requestFocus();
System.out.println("Focus gained");
}
});

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}

public static void main(String[] args) {
new Test01();
}
}


joejoe1991 2008-04-28
  • 打赏
  • 举报
回复
假设 第三个JTextField 名字叫:txt3
在显示界面之前,调用

txt3.requestFocus();

就可以了
Inhibitory 2008-04-28
  • 打赏
  • 举报
回复
对主框口处理焦点事件:public void addFocusListener(FocusListener l),

void focusGained(FocusEvent e)
Invoked when a component gains the keyboard focus.
中第三个文本框使用requestFocus函数来获得焦点, 这样就可以了
zdblzwj 2008-04-28
  • 打赏
  • 举报
回复
帮顶

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧