java java检测点击的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2901720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java detect clicked buttons
提问by user253530
I have multiple panels on a JFrame window. I am going to populate each panel differently every time. For example: i start the GUI: (image center panel, right panel, bottom panel). Center panel is populated with 20 buttons, right panel with 10 buttons and bottom panel with 3.
我在 JFrame 窗口上有多个面板。我每次都会以不同的方式填充每个面板。例如:我启动 GUI:(图像中心面板,右侧面板,底部面板)。中央面板有 20 个按钮,右侧面板有 10 个按钮,底部面板有 3 个。
second start of the GUI (same gui). Center panel has 50 buttons, right panel has 12 buttons, bottom has 3.
GUI 的第二次启动(相同的 gui)。中央面板有50个按钮,右侧面板有12个按钮,底部有3个。
So everytime there is a random number of buttons, impossible to be all uniquely named. Given the fact that I don't have a unique name for each button (just a list) I would like to know which buttons were clicked according to the panel they belong to. is that possible?
所以每次都有随机数量的按钮,不可能全部唯一命名。鉴于我没有每个按钮的唯一名称(只是一个列表),我想知道根据它们所属的面板单击了哪些按钮。那可能吗?
回答by I82Much
Somehow the buttons are being created; Let's assume you are somehow numbering them in a way you can retrieve later.
不知何故,按钮正在被创建;让我们假设您以某种方式对它们进行编号,以便稍后检索。
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JButton;
public class ButtonTest extends JFrame implements ActionListener {
public ButtonTest() {
super();
initGUI();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private final List<JButton> buttons = new ArrayList<JButton>();
private static final int NUM_BUTTONS = 20;
public void initGUI() {
JPanel panel = new JPanel();
for (int i = 0; i < NUM_BUTTONS; i++) {
String label = "Button " + i;
JButton button = new JButton(label);
button.setActionCommand(label);
button.addActionListener(this);
buttons.add(button);
panel.add(button);
}
getContentPane().add(panel);
}
public static void main(String[] args) {
new ButtonTest();
}
public void actionPerformed(ActionEvent e) {
String actionCommand = ((JButton) e.getSource()).getActionCommand();
System.out.println("Action command for pressed button: " + actionCommand);
// Use the action command to determine which button was pressed
}
}
回答by camickr
The ActionEvent has a getSource() method which will be the reference to the button that was clicked. You can then check the action command of the button if you need to.
ActionEvent 有一个 getSource() 方法,该方法将作为对被单击按钮的引用。然后,如果需要,您可以检查按钮的操作命令。
回答by Etaoin
If you want to know which panel contains the button, try calling getParent()on the JButton itself. To find out which button was clicked, as camickr suggests, use getSource()on the ActionEvent.
如果您想知道哪个面板包含按钮,请尝试调用getParent()JButton 本身。要找出点击了哪个按钮,正如 camickr 建议的那样,getSource()在 ActionEvent 上使用。

