[英]How Restart My Bingo Board when I clicked Button
我搜索了“如果按下Button时如何重新启动GUI”之类的内容,但更改了源代码,但是它不起作用。 我想在单击按钮时将数字更改为“ X”。
Container c = getContentPane();
c.setLayout(new GridLayout(5,5));
JB = new JButton[25];
for(int i=0; i<1000; i++)
{
j = (int)(Math.random()*25);
temp = nums[0];
nums[0] = nums[j];
nums[j] = temp;
}
for(int i=0; i<JB.length; i++)
{
JB[i] = new JButton(nums[i]);
JB[i].addActionListener(this);
c.add(JB[i]);
}
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
for(int i=0; i<nums.length; i++)
{
if (e.getSource() == JB[i])
{
System.out.println(nums[i]);
***nums[i] = "X"; //I want show "X" when I clicked a Button***
JB[i] = new JButton(nums[i]);
restart();
}
}
}
public void restart() // I deleted.
{
start();
}
尝试这个:
JB[i].setText(nums[i]);
代替这个:
JB[i] = new JButton(nums[i]);
更改按钮文本。 无需重新启动(我想这就是您要执行的操作?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.