获得焦点和失去焦点 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16947532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:28:30  来源:igfitidea点击:

Focus Gained and Focus Lost Java

java

提问by bentinata

I have some text field on my form. And I like focusGained and focusLost event. Doing this with 2 or 3 text field is easy. But, after 18 text field, well, it's kinda confusing. Is there any way to shorten the focusGained and focusLost event? Example:

我的表单上有一些文本字段。我喜欢 focusGained 和 focusLost 事件。使用 2 或 3 个文本字段执行此操作很容易。但是,在 18 个文本字段之后,这有点令人困惑。有没有办法缩短 focusGained 和 focusLost 事件?例子:

txtSomeTextFocusGained(java.awt.event FocusEvent evt){
    if(txtSomeText.getText().equals("Text here!"){
        txtSomeText.setText("");
    }
}

txtSomeTextFocusLost(java.awt.event FocusEvent evt){
    if(txtSomeText.getText().equals(""){
        txtSomeText.setText("Text here!");
    }
}

That's one text field, I've problem handling with about 18 text field. Any way to simplify that? Thanks.

那是一个文本字段,我在处理大约 18 个文本字段时遇到了问题。有什么办法可以简化吗?谢谢。

回答by gkalpak

The methods are simple enough, so I can't think of a way to simplify them any further. What you cando, though, is prevent code repetition by declaring oneFocusListener instance and then add it using addFocusListener(...)to alltext-fields.

方法很简单,所以我想不出进一步简化它们的方法。但是,您可以通过声明一个FocusListener 实例然后将其添加addFocusListener(...)所有文本字段来防止代码重复。

It would look something like this:

它看起来像这样:

// Instantiate a FocusListener ONCE
java.awt.event.FocusListener myFocusListener = new java.awt.event.FocusListener() {
    public void focusGained(java.awt.event.FocusEvent focusEvent) {
        try {
            JTextField src = (JTextField)focusEvent.getSource();
            if (src.getText().equals("Text here!") {
                src.setText("");
            }
        } catch (ClassCastException ignored) {
            /* I only listen to JTextFields */
        }
    }

    public void focusLost(java.awt.event.FocusEvent focusEvent) {
        try {
            JTextField src = (JTextField)focusEvent.getSource();
            if (src.getText().equals("") {
                src.setText("Text here!");
            }
        } catch (ClassCastException ignored) {
            /* I only listen to JTextFields */
        }
    }
};

(You could omit the try-catchblocks if you were absolutelysure that the source of the event would always be a JTextField, but it is always a bad practice to rely on such assumptions.)

try-catch如果您绝对确定事件源始终是 JTextField,则可以省略这些块,但依赖此类假设始终是一种不好的做法。)

Then, for every JTextField you only need to add the sameFocusListener:

然后,对于每个 JTextField,您只需要添加相同的FocusListener:

...
someTextField.addFocusListener(myFocusListener);
...

(It's only half a line - difficult to get any shorter than that.)

(它只有半行 - 很难比这更短。)



Another alternative would be to subclass JTextField, adding a FocusListener in the constructor, but I don't see any advantage over the first solution (unless you want a more flexible/powerful solution, e.g. different text for each JTextField etc).

另一种选择是子类化 JTextField,在构造函数中添加一个 FocusListener,但我看不出比第一个解决方案有什么优势(除非你想要一个更灵活/更强大的解决方案,例如每个 JTextField 等的不同文本)。

回答by Branislav Lazic

If you want just to set some text in field which gets focused you could write separated event handler class which implements FocusListenerand then override focusGainedand focusLostmethods. Something like this:

如果您只想在获得焦点的字段中设置一些文本,您可以编写分离的事件处理程序类,该类实现FocusListener然后覆盖focusGainedfocusLost方法。像这样的东西:

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextField;

public class CustomFocusListener implements FocusListener {
    JTextField txt;
    String textWhenFocusGained;
    String textWhenFocusLost;

    public CustomFocusListener(JTextField txt, String textWhenFocusGained,
            String textWhenFocusLost) {
        this.txt = txt;
        this.textWhenFocusGained = textWhenFocusGained;
        this.textWhenFocusLost = textWhenFocusLost;
    }

    @Override
    public void focusGained(FocusEvent arg0) {
        txt.setText(textWhenFocusGained);
    }

    @Override
    public void focusLost(FocusEvent arg0) {
        txt.setText(textWhenFocusLost);
    }


}

回答by Asuna

Use this.

用这个。

if(txtSomeText.getText().equals("Text here!")){
    txtSomeText.setText("");