JButton释放动作 [英] JButton Release Action

查看:69
本文介绍了JButton释放动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JButton发行版上创建操作,但不确定如何完成此操作.按下按钮时,我可以做一个很好的动作.按下按钮时,它将图像更改为红点,释放按钮时,图像应更改为默认的绿色点.

I am trying to create an action on a JButton release and I am not sure how to accomplish this. I can make an action just fine when the button is pressed. When the button is pressed it will change the image to a red dot and when released it should change back to a default green dot.

如果有人可以向我指出释放按钮时如何创建动作的方向,那么我的按钮按下代码将在下面,这将是最有帮助的.谢谢!

My button press code is below if someone can point me in the direction on how to create an action when the button is released that would be most helpful. Thanks!

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == change1) {

        p1a.setIcon(CLR); // THIS IS THE IMAGE WHEN BUTTON PRESSED
        // WOULD LIKE TO CHANGE BACK TO DEFAULT IMAGE HERE WHEN BUTTON IS RELEASED
    }
}

推荐答案

按下按钮时,会将图像更改为红点,释放按钮时,图像应更改为默认的绿点.

When the button is pressed it will change the image to a red dot and when released it should change back to a default green dot.

这可以完全在JButton中实现.参见 setPressedIcon(Icon) ..

This can be entirely achieved within the JButton. See things like setPressedIcon(Icon) ..

E.G.

import javax.swing.*;
import java.net.*;

public class ButtonIcons {

    ButtonIcons() throws MalformedURLException {
        ImageIcon redIcon = new ImageIcon(new URL(
                "https://i.stack.imgur.com/wCF8S.png"));
        ImageIcon grnIcon = new ImageIcon(new URL(
                "https://i.stack.imgur.com/T5uTa.png"));
        JButton button = new JButton("Click me!", grnIcon);
        button.setPressedIcon(redIcon);
        JOptionPane.showMessageDialog(null, button);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                ButtonIcons o = new ButtonIcons();
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于JButton释放动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆