如何禁用textField上的自动焦点

时间:2015-05-18 17:13:03

标签: java focus jtextfield

当我创建一堆JTextFields时,我看到第一个被选中。我想取消选择它,因为我有焦点监听器并且它自动运行。
有任何线索吗?

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);

对另一个文本字段重复该过程

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);


}
}

我想将焦点设置为窗口或其他,以防止文本消失。

3 个答案:

答案 0 :(得分:1)

在构造函数中,您可以使用方法requestFocusInWindow()

这就是我在这里工作的东西 -

创建JFrame后,请致电frame.requestFocusinWindow();。这将确保您的文本字段不会聚焦。 然后,当您专注于文本字段时,事件将被触发。

答案 1 :(得分:1)

正如评论中所讨论的,我添加了一个单选按钮来代替焦点:

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);
    }
}

解释

来自tutorial

  

如果要在第一次激活窗口时确保特定组件获得焦点,则可以在实现组件之后但在显示帧之前调用组件上的requestFocusInWindow方法。

这意味着btn.requestFocusInWindow()必须在pack()之后和a.setVisible(true)之前出现。

你需要另一个组件来关注焦点的原因是当一个窗口被聚焦时,它内部的一个组件必须获得焦点。

注意:

  • 如果您想要更好的文字字段提示,请参阅@camickr's answer
  • 请勿使用null布局。选择一个为你的GUI设计服务的(我选择FlowLayout只是因为它使用起来很快,但可能不是你需要的。)
  • 在添加完所有组件后,pack()而不是设置框架的大小。
  • 不要为每个文本字段创建相同的焦点侦听器,只需将其创建为类并重用它即可。我展示了将组件传递给构造函数的一种方法,但是你可以摆脱它并使用e.getComponent()来获取文本字段实例。

答案 2 :(得分:0)

tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field

也许您正在尝试为文本字段获得焦点时消失的文本字段设置提示?

如果是,请查看Text Field Prompt以获取解决方案。

如果没有,那么发布一个合适的SSCCE,因为我仍然猜不出你想要做什么。

相关问题