首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用取消按钮时恢复InputVerifier机制?

如何在使用取消按钮时恢复InputVerifier机制?
EN

Stack Overflow用户
提问于 2017-11-09 02:36:00
回答 1查看 123关注 0票数 0

我在JFormattedTextField上有一台InputVerifier。当字段完全失去焦点时,它被调用。我添加了一个cancel按钮(JButton),但不想在单击此按钮时调用InputVerifier。

当焦点在字段上丢失而没有任何问题时,将调用MyVerifier。但是,在我单击clearButton之后,InputVerifier将不会再次被调用,就像没有被单击的按钮一样。

完整的代码是:

代码语言:javascript
复制
public class DemoFrame extends javax.swing.JFrame {

    /** Creates new form DemoFrame */
    public DemoFrame() {
        initComponents();
        name.setValue("");
        name.setInputVerifier(new MyVerifier());
        clear.setVerifyInputWhenFocusTarget(false);
    }

    private class MyVerifier extends javax.swing.InputVerifier {
     public boolean verify(javax.swing.JComponent input) {
         System.out.println("Field is being verified");
          return true;
      }
      public boolean shouldYieldFocus(javax.swing.JComponent input) {
          return verify(input);
      }
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        name = new javax.swing.JFormattedTextField();
        clear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        name.setText(" ");

        clear.setText("clear");
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(231, Short.MAX_VALUE)
                .addComponent(clear)
                .addGap(112, 112, 112))
            .addGroup(layout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(156, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addComponent(clear)
                .addGap(73, 73, 73))
        );

        pack();
    }// </editor-fold>

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {
        name.setValue(null);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DemoFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton clear;
    private javax.swing.JFormattedTextField name;
    // End of variables declaration
}

如果字段在未单击按钮的情况下失去焦点,为什么不调用InputVerifier?

EN

回答 1

Stack Overflow用户

发布于 2017-11-09 03:19:39

井。我找到了解决方案。

代码语言:javascript
复制
    clear.setFocusable(false);

这就解决了问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47187210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档