我现在在一个版面上有4个JTextField,现在我想让界面在跳出的时候就让光标停在第三个文本框里,请问应该怎么做呢

解决方案 »

  1.   

    对主框口处理焦点事件:public void addFocusListener(FocusListener l), 

    void focusGained(FocusEvent e) 
              Invoked when a component gains the keyboard focus. 
    中第三个文本框使用requestFocus函数来获得焦点, 这样就可以了
      

  2.   

    假设 第三个JTextField 名字叫:txt3
    在显示界面之前,调用txt3.requestFocus();就可以了
      

  3.   

    写了个小例子, 用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();
        }
    }
      

  4.   

    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 
      

  5.   

    试试 txt.setSelectedStart(0); txt.setSelectedEnd(txt.getText().length());