I am creating a Tic-Tac-Toe game using Java. Right now I have this, when you click on the button that the JButton will be removed from JPanel , a JLabel containing either an X or O image will be added, and the JPanel will be repainted. However, when I click the button, the image does not appear, but the button disappears.
Button Creation and JLabel / Image :
package tictactoe; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.ImageIcon; public class TicTacToe implements ActionListener { private JFrame holder = new JFrame(); private GridLayout layout = new GridLayout(3,3); private FlowLayout panel = new FlowLayout(FlowLayout.CENTER); private JPanel p11, p12, p13, p21, p22, p23, p31, p32, p33; private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9; private ImageIcon iconX = new ImageIcon("iconX.png"); private JLabel xLabel = new JLabel(iconX); private ImageIcon iconO = new ImageIcon("iconO.png"); private JLabel oLabel = new JLabel(iconO); private int turn; private char s1, s2, s3, s4, s5, s6, s7, s8, s9; public TicTacToe() { paint(); } private void paint() { holder.setLayout(layout); holder.setSize(300,300); b1 = new JButton("1"); p11 = new JPanel(); p11.setLayout(panel); p11.add(b1); holder.add(p11);

source share