【问题标题】:Java JTextField ActionListener check if the field has an inputJava JTextField ActionListener 检查字段是否有输入
【发布时间】:2014-10-19 15:56:41
【问题描述】:

我有这个代码:

    static JFrame inputFrame = new JFrame();
    static JTextField myTextfield = new JTextField();
    static JButton myButton = new JButton("Hi!");

private static void inputGUI() {
    inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inputFrame.setTitle("The INPUT");
    inputFrame.setLayout(new FlowLayout());
    inputFrame.setSize(1366,768);
    inputFrame.setVisible(true);

    inputFrame.add(myTextField);
    inputFrame.add(myButton);

    myButton.setEnabled(false);
    myTextField.addActionListener(myListener);
}
static ActionListener myListener = new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==inputFrame){
            //What Do I put here?
        }
    }
}

我想做的是:
当我在myTextField 中输入内容而不按回车时,按钮myButton 被启用(例如myButton.setEnabled(true);)。

【问题讨论】:

标签: java validation input actionlistener jtextfield


【解决方案1】:

The best solution 正在使用 DocumentFilter 。因此它可以检查您的JTextField 何时包含某些内容,然后您可以触发您的按钮处于活动状态。

在这里,我重新修改了您的代码:

  import java.awt.*;
  import javax.swing.*;
  import javax.swing.text.AbstractDocument;
  import javax.swing.text.AttributeSet;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.DocumentFilter;

class checkText extends DocumentFilter {
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
        AttributeSet attrs) throws BadLocationException {
        super.replace(fb, offset, length, text, attrs);
       GuiTest.enableButton();
       }
      }

 public class GuiTest extends JFrame {
static JFrame inputFrame = new JFrame();
static JTextField myTextfield = new JTextField(10);
static JButton myButton = new JButton("Hi!");

public GuiTest() {
    inputGUI();
}

private static void inputGUI() {
    inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inputFrame.setTitle("The INPUT");
    inputFrame.setLayout(new FlowLayout());
    inputFrame.setSize(1366, 768);
    inputFrame.setVisible(true);

    inputFrame.add(myButton);

    DocumentFilter filter = new checkText();
    ((AbstractDocument) myTextfield.getDocument()).setDocumentFilter(filter);
    inputFrame.add(myTextfield);
    myButton.setEnabled(false);
}

public static void enableButton() {
    myButton.setEnabled(true);
}

public static void main(String args[]) { new GuiTest(); }
}

之前:

之后:

【讨论】:

    【解决方案2】:

    试试这个

    myTextField.addKeyListener(new java.awt.event.KeyAdapter() {
    
            public void keyPressed(java.awt.event.KeyEvent evt) {
    
                if(myTextField.getText().isEmpty()){
                    myButton.setEnabled(false);
                 } else{
                    myButton.setEnabled(true);
                 }
                }
             });
    

    希望对你有帮助!

    【讨论】:

    • 他可能使用的最糟糕的解决方案。
    • @BranislavLazic 为什么使用 keylistner 是最糟糕的解决方案?
    • @ThusithaThilinaDayaratne 如果 JTextField 中的文本从其他组件而不是您的键盘获取文本属性的更新怎么办?
    • @BranislavLazic 哦,我完全忘记了:/
    • @ThusithaThilinaDayaratne 哈哈!无需道歉。这是个好问题。 :)
    【解决方案3】:

    您只需插入以下代码:

    JTextField jTF = (JTextField)e.getSource();
    if( jTF.getText().length()>0 )
        myButton.setEnabled(true);
    

    回答cmets:

    • 读取 isEmpty() 的 javadoc,当且仅当 length==0 时返回 true,所以是一样的。
    • 我没有放任何cmets,因为最初的问题是“我在这里放什么代码”,所以我发布了代码,首先真的没有错。

    【讨论】:

    • length()>0?你应该使用!isEmpty()
    • 请解释 OP 的代码有什么问题以及为什么可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多