I wrote this program and I want it to change the button text when the buttons(groundHog) are clicked. I try to use button.setText, but when i run the program, it has no effect on my buttons. I made all my buttons in a JPanel, is it the problem? I labeled where i used setText in my code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GroundHog extends JPanel{
Controller controller = new Controller();
public static JButton [] groundHog = new JButton[9];
GroundHog(){
setLayout(new BorderLayout());
setPreferredSize(new Dimension(500, 350));
add(createGamePanel(), BorderLayout.LINE_START);
add(createLabelPanel(), BorderLayout.LINE_END);
}
public JPanel createGamePanel(){
JPanel panelGame = new JPanel();
panelGame.setPreferredSize(new Dimension(300, 310));
JButton background = new JButton("background");
for(int x = 0; x <= 8; x ++){
groundHog[x] = new JButton("g" + x);
groundHog[x].addActionListener(new ButtonActionListener());
groundHog[x].setPreferredSize(new Dimension(75,75));
groundHog[x].setVerticalAlignment(SwingConstants.CENTER);
groundHog[x].setHorizontalAlignment(SwingConstants.CENTER);
groundHog[x].setLayout(new FlowLayout());
background.add(groundHog[x]);
}
background.addActionListener(new ButtonActionListener());
background.setPreferredSize(new Dimension(300,300));
background.setVerticalAlignment(SwingConstants.CENTER);
background.setHorizontalAlignment(SwingConstants.CENTER);
background.setLayout(new FlowLayout());
panelGame.add(background);
return panelGame;
}
private JPanel createLabelPanel(){
JLabel scoreLabel = new JLabel("Score\t");
JLabel missLabel = new JLabel("Miss\t");
JButton startButton = new JButton("start");
JPanel panelLabel = new JPanel();
panelLabel.setPreferredSize(new Dimension(150, 310));
scoreLabel.setPreferredSize(new Dimension(100, 50));
scoreLabel.setVerticalAlignment(SwingConstants.CENTER);
scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
scoreLabel.setLayout(new FlowLayout());
panelLabel.add(scoreLabel);
missLabel.setPreferredSize(new Dimension(100, 50));
missLabel.setVerticalAlignment(SwingConstants.CENTER);
missLabel.setHorizontalAlignment(SwingConstants.CENTER);
missLabel.setLayout(new FlowLayout());
panelLabel.add(missLabel);
startButton.addActionListener(new ButtonActionListener());
startButton.setPreferredSize(new Dimension(100, 50));
startButton.setVerticalAlignment(SwingConstants.CENTER);
startButton.setHorizontalAlignment(SwingConstants.CENTER);
startButton.setLayout(new FlowLayout());
panelLabel.add(startButton);
return panelLabel;
}
private JMenuBar createMenuBar(){
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu();
JMenuItem menuItem = new JMenuItem("changeMusic");
menuItem.addActionListener(new MenuActionListener());
menuBar.add(menu);
return menuBar;
}
private class ButtonActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Image image = new Image();
String command = e.getActionCommand();
if(command.equals("g0")){
controller.hitGroundHog();
*//I want to change the button's text here, but does not do anything*
groundHog[0].setText("hello");
groundHog[0] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g1")){
controller.hitGroundHog();
groundHog[1] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g2")){
controller.hitGroundHog();
groundHog[2] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g3")){
controller.hitGroundHog();
groundHog[3] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g4")){
controller.hitGroundHog();
groundHog[4] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g5")){
controller.hitGroundHog();
groundHog[5] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g6")){
controller.hitGroundHog();
groundHog[6] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g7")){
controller.hitGroundHog();
groundHog[7] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("g8")){
controller.hitGroundHog();
groundHog[8] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("background")){
controller.missGroundHog();
groundHog[9] = new JButton(image.beenHitGroundHogIcon());
}else if(command.equals("start")){
controller.startGame();
}
}
}
private class MenuActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
private static void createAndShowGUI(){
GroundHog gH = new GroundHog();
JFrame frame = new JFrame("GroundHog");
frame.add(gH.createMenuBar());
frame.getContentPane().add(new GroundHog());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
GroundHog ground = new GroundHog();
frame.setJMenuBar(ground.createMenuBar());
frame.setSize(450, 260);
frame.setVisible(true);
}
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
}
}