java执行按钮_java-通过热键执行相应操作时的动画按钮

这对我有用(不是很优雅但是可以用)

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.AbstractAction;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.KeyStroke;

import javax.swing.Timer;

public class Test extends JFrame {

public static void main(String[] args) {

Test t = new Test();

final JButton button = new JButton();

AbstractAction action = new AbstractAction("Hello World!") {

@Override

public void actionPerformed(ActionEvent e) {

button.getModel().setArmed(true);

button.getModel().setPressed(true);

Timer t = new Timer(200, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

button.getModel().setArmed(false);

button.getModel().setPressed(false);

}

});

t.setRepeats(false);

t.start();

// Do action stuff

}

};

button.setAction(action);

JPanel panel = new JPanel();

panel.add(button);

panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ctrl ENTER"), action);

panel.getActionMap().put(action, action);

t.add(panel);

t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

t.pack();

t.setVisible(true);

}

}

Pressing the mouse on top of a button makes the model both armed and pressed. As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button). A button is triggered, and an ActionEvent is fired, when the mouse is released while the model is armed – meaning when it is released over top of the button after the mouse has previously been pressed on that button (and not already released). Upon mouse release, the model becomes unarmed and unpressed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值