我搜索了"如果我按下按钮如何重新启动我的 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]);
以更改按钮文本。无需重新启动(我认为这就是您要做的?