【发布时间】:2017-04-02 02:12:49
【问题描述】:
这是我的代码:
for(int row = 0; row < 10; row++)
{
for(int col = 0; col < 10; col++)
{
button = new JButton();
panel_1.add(button);
}
}
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//If Button is clicked, make the button unclickable
if(button == (JButton)e.getSource())
{
button.setEnabled(false);
}
}
});
我想让我从这个 10 x 10 网格按钮布局中单击的任何 JButton 都无法单击;但是,这种方法只会使右键无法单击,这是怎么回事?我已将 ActionListener 放在负责制作按钮的 for 循环之外。不知道怎么回事。
这是它的样子:
编辑:bsd 代码有效。在添加按钮或其他内容之前添加 ActionListener。
【问题讨论】: