[英]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.