简体   繁体   English

Java Action Listener从源更改对象

[英]Java Action Listener Change Object From Source

I want to change the icon on a button based on what i clicked. 我想根据我单击的内容更改按钮上的图标。 The issue is i have an array and it is shuffled so i need a way of changing the icon based on what one of my 30 buttons i clicked. 问题是我有一个数组,它被打乱了,所以我需要一种基于单击的30个按钮中的一个来更改图标的方法。

For example something like this 例如这样的事情

if (source == "dos")
{

change my icon on what button i clicked to this (icon)


}

i need a way of changing the icon based on what one of my 30 buttons i clicked. 我需要一种基于单击的30个按钮中的一个来更改图标的方法。

You get the button that was clicked from the ActionEvent of your ActionListener : 您将获得从ActionListenerActionEvent单击的按钮:

JButton button = (JButton)event.getSource();
button.setIcon(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM