获取按钮名称 [英] Getting the Button name
问题描述
嘿,朋友们,我在处理按钮时有疑问.我想在actionPerformed()方法中使用按钮的名称.以下是将更清楚地描述我的问题的代码.
hey frnds I am having a doubt in handling the buttons. I want to use the name of the button in the actionPerformed() method. Following is the code which will describe my question more clearly.
class myframe extends JFrame implements ActionListener
{
//ArrayList <jbutton> blist;
private static final long serialVersionUID = 1L;
//private static final String EXIT_ON_CLOSE = null;
int i=0,j=0;
int flag[];
JButton b[][]=new JButton[4][4];
myframe()
{
super("Queens Problem");
//b[] =new JButton[16];
Image img=getToolkit().createImage("images/queen.jpg");
ImageIcon imgicn=new ImageIcon(img);
try
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
b[i][j]=new JButton();
b[i][j].setIcon(imgicn);
add(b[i][j]);
b[i][j].addActionListener(this);
}
}
}
catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
setLayout(new GridLayout(4,4));
setSize(500,500);
setVisible(true);
// setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent a)
{
//setBackground(Color.BLUE);
//UIManager.put("Button.background", Color.red);
JButton button = (JButton) a.getSource();
button.setBackground(Color.red);
//a.getComponent().setBackground(Color.red);
}
}
在上面的actionPerformed方法中,我想获取被单击的按钮的名称(即b [i] [j]),以便可以对该按钮执行操作.如何避免使用getSource()?
提出一些建议.谢谢.
In the actionPerformed method above, i want to get the name of the button(ie b[i][j]) whichever is clicked so that i can perform operations on that button.How can i avoid the use of getSource()?
Give some suggestions. Thanks.
推荐答案
您应该使用getSource();
.您还应该首先使用instanceof
:
检查从该函数给出的对象是否真的是一个Button.
You should usegetSource();
. You should also check first if the object, that''s given from that function is really a Button by usinginstanceof
:
if(a instanceof JButton){
JButton button = (JButton) a.getSource();
if(button.getText() == "Funny button"){
button.setBackground(Color.red);
}
}
鉴于每个按钮都有不同的文本,因此您可以随时识别组件.
只要您只有几个控件,此方法就起作用.否则
我建议根据您的需求自定义组件:
This way you''re able to identify your components at any time - given that every button has a different text.
This works as long as you only have a few controls. Otherwise
I recommend to customize the components for your needs:
public class MyButton extends JButton {
private final String strButtonID;
public MyButton(String strButtonText, String strButtonID){
super(strButtonText);
this.strButtonID = strButtonID;
}
public String getID(){
return strButtonID;
}
}
如果您还对ID使用定义了静态字符串的接口,即使两个按钮具有相同的文本,您也可以节省:
If you then also use a Interface with defined static Strings for the IDs you''re pretty save even when 2 button have the same text:
public interface IComponentID{
public static final String BUTTONID = "FunnyButton";
}
然后,您只需要这样做:
You then only need to do it like this:
MyButton oButton = new MyButton("Buttontext", IComponentID.BUTTONID);
....
if(a instanceof MyButton){
MyButton button = (MyButton) a.getSource();
if(button.getID() == IComponentID.BUTTONID){
button.setBackground(Color.red);
}
}
并且-现在我们已经走了很长一段路-我建议不仅设置一个字符串来标识按钮,而且还建议使用ComponentID(String ID,String Buttontext).
但我认为您可以开发;-)
AND - now that we''ve come this long way - I would recommend to not just set an String to identify the button but a ComponentID(String ID, String Buttontext).
But I assume you''re able to develop that ;-)
这篇关于获取按钮名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!