【问题标题】:What is the equivalent of button3_Click(sender, e); in Java?button3_Click(sender, e) 的等价物是什么?在 Java 中?
【发布时间】:2012-07-13 04:34:46
【问题描述】:

在 Java 中调用button3_Click(sender, e); 的作用是什么?

我正在尝试使文本字段操作(按 Enter)触发按钮的代码。

谢谢!

【问题讨论】:

    标签: java swing action call jbutton


    【解决方案1】:
    final JButton btn = new JButton("Click Me!");
    JTextField txt = new JTextField(10);
    InputMap inputMap = txt.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = txt.getActionMap();
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), 13);
    actionMap.put(13, new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            btn.doClick();
        }
    });
    

    【讨论】:

      【解决方案2】:

      对 TextField 使用 TextListener 之类的

      public void textValueChanged(TextEvent e) { 
        // call doClick();
      }
      

      对于按钮使用 ActionListener

      public void actionPerformed(ActionEvent e) {
      }
      

      调用doClick()方法参考 http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#doClick%28%29

      【讨论】:

      • 请将 TextListener 改为 DocumentListener
      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 2023-03-07
      • 2021-10-18
      • 2010-12-07
      • 2013-03-12
      • 2014-12-06
      • 2011-06-05
      相关资源
      最近更新 更多