Java Textfield focus

后端 未结 2 723
野的像风
野的像风 2021-01-15 12:12

Hello I have a problem with the focus

mytext= new JTextField();
mytext.requestFocus(true);
gc.fill =GridBagConstraints.HORIZONTAL ;
gc.gridx =3; gc.gridy=4;
         


        
相关标签:
2条回答
  • 2021-01-15 12:28

    From the Swing Tutorial

    If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:

    //...Where initialization occurs...
    JFrame frame = new JFrame("Test");
    JPanel panel = new JPanel(new BorderLayout());
    
    //...Create a variety of components here...
    
    //Create the component that will have the initial focus.
    JButton button = new JButton("I am first");
    panel.add(button);
    frame.getContentPane().add(panel);  //Add it to the panel
    
    frame.pack();  //Realize the components.
    //This button will have the initial focus.
    button.requestFocusInWindow(); 
    frame.setVisible(true); //Display the window.
    
    0 讨论(0)
  • 2021-01-15 12:47

    As for selecting all the text you should use...

    mytext.selectAll();
    

    As for getting focus, maybe you should try the requestFocus function after everything has been added to jContentPane.

    0 讨论(0)
提交回复
热议问题