【问题标题】:Java Textfield focusJava 文本字段焦点
【发布时间】:2010-01-30 11:45:20
【问题描述】:

您好,我的焦点有问题

mytext= new JTextField();
mytext.requestFocus(true);
gc.fill =GridBagConstraints.HORIZONTAL ;
gc.gridx =3; gc.gridy=4;
gbl.setConstraints(mytext,gc);
jContentPane.add(mytext);

我试过了

mytext.requestFocus();

如何自动选择文本字段中的文本以便标记文本?

【问题讨论】:

    标签: java swing focus


    【解决方案1】:

    来自Swing Tutorial

    如果要确保特定组件在第一次激活窗口时获得焦点,可以在组件实现后但在框架显示之前调用该组件的 requestFocusInWindow 方法。以下示例代码显示了如何完成此操作:

    //...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.
    

    【讨论】:

      【解决方案2】:

      至于选择你应该使用的所有文本......

      mytext.selectAll();
      

      至于获得焦点,也许你应该在所有内容都添加到 jContentPane 之后尝试 requestFocus 函数。

      【讨论】:

      猜你喜欢
      • 2010-11-09
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多