简体   繁体   English

如何禁用对textField的自动聚焦

[英]How to disable automatic focus on textField

When I was creating a bunch of JTextFields I saw that first one is selected. 当我创建一堆JTextFields我看到第一个被选中。 I want to deselect it, because I have focus listener and it's running automatically. 我想取消选择它,因为我有焦点侦听器并且它正在自动运行。
Any clues? 有什么线索吗?

SSCCE: SSCCE:

JTextField tf = new JTextField("hello");
tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field
textFieldKwotaWplacona.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) 
        {

            if(tf.getForeground() != Color.BLACK)
            {
            tf.setText("");
            tf.setForeground(Color.BLACK);
            }
        }   @Override
        public void focusLost(FocusEvent arg0) {}});
//for deleting "nice comment" after click

tf.setBounds(//some bounds);
add(tf);

Repeat that process for another text field 对另一个文本字段重复该过程

EDIT2 : actual code (I believe its sscce :P) EDIT2:实际代码(我相信它的sscce:P)

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Main extends JFrame implements ActionListener
{
JTextField textFieldKwotaWplacona, textFieldOprocentowanie, textFieldDlugoscLokaty, textFieldKwotaOtrzymana;

Main()
{   setSize(500,300);
    setLayout(null);
    setTitle("Program do liczenia procentu składanego");
setDefaultCloseOperation(EXIT_ON_CLOSE);


    textFieldKwotaWplacona = new JTextField("Ilość pieniędzy wpłaconych");
    textFieldKwotaWplacona.setForeground(Color.decode("0x8C8C8C"));
    textFieldKwotaWplacona.addActionListener(this);
    textFieldKwotaWplacona.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) 
        {

            if(textFieldKwotaWplacona.getForeground() != Color.BLACK)
            {
            textFieldKwotaWplacona.setText("");
            textFieldKwotaWplacona.setForeground(Color.BLACK);
            }
        }   @Override
        public void focusLost(FocusEvent arg0) {}});

    textFieldKwotaWplacona.setBounds(10, 10, 100, 20);
    add(textFieldKwotaWplacona);

    textFieldOprocentowanie = new JTextField("Oprocentowanie");
    textFieldOprocentowanie.setForeground(Color.decode("0x8C8C8C"));
    textFieldOprocentowanie.addActionListener(this);


    textFieldOprocentowanie.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) 
        {

            if(textFieldOprocentowanie.getForeground() != Color.BLACK)
            {
            textFieldOprocentowanie.setText("");
            textFieldOprocentowanie.setForeground(Color.BLACK);
            }
        }

        @Override
        public void focusLost(FocusEvent arg0) {}});
    textFieldOprocentowanie.setBounds(10, 40, 100, 20);
    add(textFieldOprocentowanie);



}




@Override
public void actionPerformed(ActionEvent arg0) 
{
    // TODO Auto-generated method stub
}

    public static void main(String[] args) 
{
    Main a=new Main();
    a.setVisible(true);


}
}

I want to set focus to window or sth else, in order to prevent text from disappearing. 我想将焦点设置为窗口或其他,以防止文本消失。

In your constructor, you could use the method requestFocusInWindow() . 在构造函数中,可以使用requestFocusInWindow()方法。

This is what was working for me here- 这就是在这里为我工作的

After creating the JFrame, call frame.requestFocusinWindow(); 创建JFrame之后,调用frame.requestFocusinWindow(); . This will make sure your text field is not focused. 这将确保您的文本字段不集中。 Then, when you focus on the text field, the event is being fired. 然后,当您专注于文本字段时,将触发该事件。

As discussed in the comments, I added a radio button to take the focus instead: 正如评论中所讨论的,我添加了一个单选按钮来代替焦点:

public class Main extends JFrame {

    JTextField textFieldKwotaWplacona, textFieldOprocentowanie;

    Main() {

        setTitle("Program do liczenia procentu składanego");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        textFieldKwotaWplacona = new JTextField("Ilość pieniędzy wpłaconych");
        textFieldKwotaWplacona.setForeground(Color.decode("0x8C8C8C"));
        textFieldKwotaWplacona.addFocusListener(new FieldFocusListener(textFieldKwotaWplacona));
        add(textFieldKwotaWplacona);

        textFieldOprocentowanie = new JTextField("Oprocentowanie");
        textFieldOprocentowanie.setForeground(Color.decode("0x8C8C8C"));
        textFieldOprocentowanie.addFocusListener(new FieldFocusListener(textFieldOprocentowanie));
        add(textFieldOprocentowanie);

        JRadioButton btn = new JRadioButton("text");
        add(btn);

        pack();
        btn.requestFocusInWindow();
    }

    private class FieldFocusListener extends FocusAdapter {

        private JTextField field;

        FieldFocusListener(JTextField field) {

            this.field = field;
        }

        @Override
        public void focusGained(FocusEvent e) {

            if (field.getForeground() != Color.BLACK) {
                field.setText("");
                field.setForeground(Color.BLACK);
            }
        }
    }

    public static void main(String[] args) {

        Main a = new Main();
        a.setVisible(true);
    }
}

Explanation 说明

From the 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. 如果要确保特定组件在第一次激活窗口时获得焦点,则可以在实现组件之后但在显示框架之前在组件上调用requestFocusInWindow方法。

That means btn.requestFocusInWindow() must appear after pack() and before a.setVisible(true) . 这意味着btn.requestFocusInWindow()必须出现在pack()a.setVisible(true)之前。

The reason you need another component to take the focus is that when a window is focused, a component inside it must gain the focus. 您需要另一个组件来获得焦点的原因是,当窗口被聚焦时,其内部的组件必须获得焦点。

Notes: 笔记:

  • If you want a better text field hint, see @camickr's answer . 如果您想要更好的文本字段提示,请参阅@camickr的答案
  • Don't use null layout. 不要使用null布局。 Pick one that serves your GUI design (I picked FlowLayout just because it's fast to use, though probably not what you need). 选择一个可以为您的GUI设计服务的设备(我之所以选择FlowLayout只是因为它使用起来很快,尽管可能不是您所需要的)。
  • Instead of setting the size of the frame, pack() after all components had been added. 添加所有组件之后,使用pack()代替设置框架的大小。
  • Instead of creating the same focus listener for every text field, just create it as a class and reuse it. 不必为每个文本字段创建相同的焦点侦听器,而只需将其创建为类并重用即可。 I show one way with passing the component to a constructor, but you can get rid of that and use e.getComponent() to get the text field instance. 我展示了一种将组件传递给构造函数的方法,但是您可以摆脱它,而使用e.getComponent()来获取文本字段实例。
tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field

Maybe you are trying to set a prompt for the text field that disappears when the text field gains focus? 也许您正在尝试为该文本字段设置提示,当该文本字段获得焦点时该提示会消失?

If so, check out Text Field Prompt for a solution. 如果是这样,请查看“ 文本字段提示 ”以获取解决方案。

If not, then post a proper SSCCE, because I still can't guess what you are trying to do. 如果没有,请发布适当的SSCCE,因为我仍然无法猜出您正在尝试做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM