简体   繁体   English

多个jradiobutton的动作监听器

[英]actionlistener for multiple jradiobutton

i have a multiple jradiobutton that is inside a for loop and i am trying to put listener on it and this is what i found: 我有一个for循环内的多个jradiobutton,我试图将侦听器放在它上面,这就是我发现的结果:

Action listener for multiple radio buttons 多个单选按钮的动作侦听器

Create two dimensional JRadioButton array like 创建二维的JRadioButton数组,例如

  JRadioButton[][] jRadioButtons = new JRadioButton[8][]; ButtonGroup bg = new ButtonGroup(); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(8, 8)); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { JRadioButton btn = new JRadioButton(); btn.addActionListener(listener); btn.setName("Btn[" + i + "," + j + "]"); bg.add(btn); panel.add(btn); // can be used for other operations jRadioButtons[i][j] = btn; } } 

Here is single ActionListener for all JRadioButtons 这是所有JRadioButton的单个ActionListener

 ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JRadioButton btn = (JRadioButton) e.getSource(); System.out.println("Selected Button = " + btn.getName()); } }; 

i kinda understand it but i still have few clarifications: 我有点理解,但我仍然有一些澄清:

  1. what's the purpose of two dimensional jradiobutton? 二维jradiobutton的目的是什么? i mean i kinda see that it is to set a name for the jradiobuttons but as far as my understanding goes, it's only for display. 我的意思是说我有点想为jradiobuttons设置一个名称,但据我所知,它仅用于显示。 yes to confirm that that is the jradiobutton you've selected but i don't get what's the purpose of it in putting actionlistener 是的,以确认这是您选择的jradiobutton,但我不知道将actionlistener用作按钮的目的是什么
  2. is the two dimensional jradiobutton really that necessary? 二维jradiobutton真的有必要吗?
  3. can i just use the name of jradiobuttons 我可以只使用jradiobuttons的名称吗

to do something like this: 做这样的事情:

if(NameOfJRadioButton.isSelected())

{

//some procedures

}

^(i can't seem to convert that into code :/) ^(我似乎无法将其转换为代码:/)

if so, how can i do it? 如果是这样,我该怎么办? or do you have any other suggestions on how to put listener for multiple jradiobuttons? 或者您对如何为多个jradiobutton放置侦听器有其他建议吗? thank you for any of your suggestions :) 谢谢您的任何建议:)

On your first and second point, the reason for the two dimensional array is unknown as it is not your code, but is not necessary at all for the use of JRadioButtons. 在第一点和第二点,二维数组的原因是未知的,因为它不是您的代码,但对于使用JRadioButtons根本不是必需的。 However it is useful to have all your buttons in some type of array, whether it be an arraylist, or a buttonGroup (swing list for buttons) for checking things with the buttons when an action is called. 但是,将所有按钮都放在某种类型的数组中很有用,无论是数组列表还是buttonGroup(按钮的摆动列表),用于在调用动作时检查按钮的内容。 eg on your 3rd point, this array list would allow you to iterate through and check which buttons have been selected and act accordingly. 例如,在第3点,此数组列表将允许您循环浏览并检查已选择了哪些按钮并采取相应的措施。

The purpose for the action listener is for executing an action when the user clicks on a button. 动作侦听器的目的是在用户单击按钮时执行动作。 The most general use for this is making it so the user is only allowed to select a certain amount of JRadioButtons or to disable them once they have been selected. 最普遍的用途是这样做,因此仅允许用户选择一定数量的JRadioButton或在选定它们后将其禁用。 eg at a character selection menu. 例如在字符选择菜单上。

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

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