this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 500); this.setVisible(true); } public static void main(String[] args) { new Test01(); } }
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
在
void focusGained(FocusEvent e)
Invoked when a component gains the keyboard focus.
中第三个文本框使用requestFocus函数来获得焦点, 这样就可以了
在显示界面之前,调用txt3.requestFocus();就可以了
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();
}
}
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