![](/img/trans.png)
[英]My Java Actionlistener won't change a JTextArea when a JButton is pressed
[英]an ActionListener that resets a game when a JButton is pressed
嗨,我正在嘗試使用此ActionListener和方法重置游戲:
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]);
}
}
}
但是,每當我按下按鈕時,都不會發生任何事情,對我如何真正重置它的任何幫助都會很棒!
如果需要,這是我的完整代碼:
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class ShinyButtonsApp extends JFrame implements ActionListener
{
private static byte ROWS =8;
ShinyButtons shiny = new ShinyButtons();
public ImageIcon[] icons =
{
new ImageIcon("RedButton.png"),
new ImageIcon("OrangeButton.png"),
new ImageIcon("YellowButton.png"),
new ImageIcon("GreenButton.png"),
new ImageIcon("BlueButton.png"),
new ImageIcon("LightGrayButton.png"),
new ImageIcon("DarkGrayButton.png"),
};
JButton[][] shinyButtons;
public ShinyButtonsApp(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(578,634);
setResizable(false);
getContentPane().setLayout(null);
shinyButtons = new JButton[8][8];
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]);
}
}
JButton red = new JButton(icons[0]);
red.setLocation(200,200);
red.setSize(69,69);
getContentPane().add(red);
//add the NEW GAME button
JButton newGame = new JButton("New Game");
newGame.setLocation(350,570);
newGame.setSize(100, 25);
getContentPane().add(newGame);
//add the QUIT button
JButton quit = new JButton("Quit");
quit.setLocation(460, 570);
quit.setSize(100,25);
getContentPane().add(quit);
//add the SCORE text field
JTextField score = new JTextField();
score.setText(Integer.toString(shiny.score));
score.setEditable(false);
score.setLocation(70, 577);
score.setSize(100,20);
getContentPane().add(score);
//add the SCORE label
JLabel scoreLabel = new JLabel("Score:");
scoreLabel.setLocation(18,537);
scoreLabel.setSize(100,100);
getContentPane().add(scoreLabel);
quit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
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]);
}
}
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String args[])
{
ShinyButtonsApp buttons;
buttons = new ShinyButtonsApp("Shiny Buttons");
buttons.setVisible(true);
}
}
將System.out.println()
添加到您的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]);
}
}
System.out.println(getContentPane().getComponentCount());
}
您不會刪除以前添加的舊按鈕。 考慮使用適當的LayoutManager,例如GridLayout。 我還要重新考慮您的設計:您不需要刪除然后重新添加按鈕。 將數據的狀態與UI分開存儲。
試試這個...創建一個變量來存儲游戲狀態:
private final ImageIcon[][] data = new ImageIcon[8][8];
在您的reset()方法中設置游戲狀態:
private void reset() { // no need to add the buttons again
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
data[row][col] = icons[(byte) (Math.random() * icons.length)];
shinyButtons[row][col].setIcon(data[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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.