gpt4 book ai didi

java - 内部类中有变量

转载 作者:行者123 更新时间:2023-12-01 12:04:13 25 4
gpt4 key购买 nike

我正在制作一个按钮网格,我希望这些按钮只被单击一次。在我的网格玻璃中,我试图在我的actionListener 中将按钮设置为setEnabled(false)。问题是我试图通过 for 循环来做到这一点,并且我有一个按钮数组,所以我需要变量(这只是代码的一部分,我取出了声明和实例化按钮的部分)

public Grid()
{

setLayout(new GridLayout(5,5));
final JButton[] buttons = new JButton[25];
for( int i = 0; i < buttons.length; i++)
{
buttons[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

String inputValue = JOptionPane.showInputDialog("Input");
buttons[i].setEnabled(false);
}
});
}



}

最佳答案

您可以通过 (JButton) e.getSource() 获取对触发事件的按钮的引用

关于java - 内部类中有变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27759994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com