Java 当按下JButton时重置游戏的ActionListener
嗨,我正在尝试使用此ActionListener和方法重置游戏:Java 当按下JButton时重置游戏的ActionListener,java,arrays,swing,user-interface,actionlistener,Java,Arrays,Swing,User Interface,Actionlistener,嗨,我正在尝试使用此ActionListener和方法重置游戏: newGame.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reset(); } }); } private void reset() { for(byte row=0; row<8
newGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
}
private void reset() {
for(byte row=0; row<8; row++)
{
for(byte col=0; col<8; col++)
{
ImageIcon randomValue = icons[(byte)(Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10+col*69, 10+row*69);
shinyButtons[row][col].setSize(69,69);
getContentPane().add(shinyButtons[row][col]);
}
}
}
newGame.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
重置();
}
});
}
私有无效重置(){
对于(byte row=0;row在reset()方法中添加一个System.out.println()
private void reset() {
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
ImageIcon randomValue = icons[(byte) (Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
System.out.println(getContentPane().getComponentCount());
}
在reset()方法中设置游戏状态:
private void reset(){//无需再次添加按钮
用于(字节行=0;行<8;行++){
for(字节col=0;col<8;col++){
数据[行][列]=图标[(字节)(Math.random()*icons.length)];
shinyButtons[row][col].setIcon(数据[row][col]);
}
}
}
首次创建按钮时,不要忘记初始化游戏状态:
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
shinyButtons[row][col] = new JButton();
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
reset();
for(字节行=0;行<8;行++){
for(字节col=0;col<8;col++){
shinybutton[row][col]=新的JButton();
shinyButtons[行][col].setLocation(10+列*69,10+行*69);
shinyButtons[row][col].setSize(69,69);
getContentPane().add(shinyButtons[row][col]);
}
}
重置();
在reset()方法中添加一个System.out.println()
,查看发生了什么:
private void reset() {
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
ImageIcon randomValue = icons[(byte) (Math.random() * icons.length)];
shinyButtons[row][col] = new JButton(randomValue);
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
System.out.println(getContentPane().getComponentCount());
}
在reset()方法中设置游戏状态:
private void reset(){//无需再次添加按钮
用于(字节行=0;行<8;行++){
for(字节col=0;col<8;col++){
数据[行][列]=图标[(字节)(Math.random()*icons.length)];
shinyButtons[row][col].setIcon(数据[row][col]);
}
}
}
首次创建按钮时,不要忘记初始化游戏状态:
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
shinyButtons[row][col] = new JButton();
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
reset();
for(字节行=0;行<8;行++){
for(字节col=0;col<8;col++){
shinybutton[row][col]=新的JButton();
shinyButtons[行][col].setLocation(10+列*69,10+行*69);
shinyButtons[row][col].setSize(69,69);
getContentPane().add(shinyButtons[row][col]);
}
}
重置();
我刚刚将System.out.println(getContentPane().getComponentCount())添加到我的代码中,它只打印了数字值:133 197 261 325 389 453 517 581我不确定这是什么means@user3330114将其视为已添加到getContentPane()的按钮数JPanel。你只是不断添加更多的按钮,从不删除它们,每次只在上面堆积一个新层。你对我如何做到这一点有什么建议吗?从数组中删除内容似乎并不容易。我只是添加了System.out.println(getContentPane().getComponentCount());到我的代码,它刚刚打印了数字值:133 197 261 325 389 453 517 581我不确定那是什么means@user3330114将其视为已添加到getContentPane()的按钮数JPanel。你只是不断添加更多的按钮,从不删除它们,每次只在上面堆积一个新的层。你对我如何做到这一点有什么建议吗?从数组中删除内容似乎并不容易。数组索引是整数,而不是字节。使用字节而不是整数会让代码变得更复杂。看看是否有帮助。Array索引是整数,而不是字节。使用字节而不是整数会使代码变得更加复杂。请查看是否有帮助。
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
shinyButtons[row][col] = new JButton();
shinyButtons[row][col].setLocation(10 + col * 69, 10 + row * 69);
shinyButtons[row][col].setSize(69, 69);
getContentPane().add(shinyButtons[row][col]);
}
}
reset();