【问题标题】:How to assign a method to a button when pressed or released with ActionListener in Java?在Java中使用ActionListener按下或释放时如何为按钮分配方法?
【发布时间】:2017-12-12 05:00:50
【问题描述】:

我是使用 Java 编写 GUI 的新手,我试图在按下按钮时在终端上打印一条消息,并在释放按钮时打印另一条消息。

这就是我经常按下按钮的方式。

 leftButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Pressed");
        }
    });

我在 IntelliJ IDEA 的帮助下完成了这项工作。我想让按钮在按下时发送消息,在释放时发送不同的东西。

【问题讨论】:

    标签: java user-interface button


    【解决方案1】:

    使用自定义类并使用它

     leftButton.getModel().addChangeListener(new BtnCusttomListener());
    
    
    
    
     private class BtnCusttomListener implements ChangeListener {
            private boolean pressed = false;  // holds the last pressed state of the button
    
            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel Buttonmodel = (ButtonModel) e.getSource();
    
                // if the current state differs from the previous state
                if (model.isPressed() != pressed) {
                    String text = "Button pressed: " + model.isPressed() + "\n"; 
                    textArea.append(text);
                    pressed = model.isPressed();
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      你可以添加一个简单的MouseAdapter,像这样:

      MouseAdapter ma = new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
              System.out.println("Pressed");
          }
          public void mouseReleased(MouseEvent e) {
              System.out.println("Released");
          }
      };
      leftButton.addMouseListener(ma);
      frame.add(button);
      

      这将检测鼠标何时按下按钮或释放按钮。

      如果需要,您还可以在MouseAdapter 中添加mouseClicked() 方法、mouseExited()mouseEntered()mouseMoved() 和(许多)更多方法。查看this JavaDoc

      【讨论】:

        【解决方案3】:

        您可以改用MouseListener

        leftButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // The mouse button was pressed and released
            }
        
            @Override
            public void mousePressed(MouseEvent e) {
                // The mouse button was pressed
            }
        
            @Override
            public void mouseReleased(MouseEvent e) {
                // The mouse button was released
            }
        
            @Override
            public void mouseEntered(MouseEvent e) {
                // The cursor entered the bounds of the button (i.e. hovering)
            }
        
            @Override
            public void mouseExited(MouseEvent e) {
                // The cursor exited the bounds of the button
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2013-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多