I searched 'How Restart my GUI if I pressed Button' or somethings and I changed my source, but it doesn't work. I want to chagne number to "X" when I clicked a Button.
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();
}
Try this:
JB[i].setText(nums[i]);
instead of this:
JB[i] = new JButton(nums[i]);
to change button text. No restart needed (I think this is what you're trying to do?)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.