0

I am making a small game of tic tac toe. I want to show an image of X's and O's instead of just putting a string when the user clicks the button. In my actionlister method i have the image added but it is not showing when I run the program. I am not sure where i am wrong.Thanks in advance,

 public class TTT extends JFrame implements ActionListener {

        private JButton buttons[] = new JButton[9];
        private JButton exitButton;
        public JLabel title;
        public JPanel titlePanel, panel;
        ;
        int count = 0;
        int symbolCount = 0;

        public TTT() {

            title = new JLabel("Welcome to my Tic Tac Toe Game!");
            titlePanel = new JPanel();
            title.setFont(new Font(Font.SERIF, 0, 30));
            titlePanel.add(title);
            this.add(titlePanel, BorderLayout.NORTH);

            panel = new JPanel(new GridLayout(3, 3));
            for (int i = 0; i < buttons.length; i++) {
                buttons[i] = new JButton();
                panel.add(buttons[i]);
                buttons[i].setEnabled(true);
                buttons[i].addActionListener(this);
            }
            this.add(panel, BorderLayout.CENTER);

            JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
            exitButton = new JButton("Quit");
            panel1.add(exitButton);
            this.add(panel1, BorderLayout.SOUTH);
            exitButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(WIDTH);
                }
            });

        }

        public static void main(String[] args) {
            TTT ref1 = new TTT();
            ref1.setTitle("Tic Tac Toe");
            ref1.setVisible(true);
            ref1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ref1.setSize(500, 500);
            ref1.setLocationRelativeTo(null);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            count++;
            for (int i = 0; i < buttons.length; i++) {
                if (buttons[i] == e.getSource()) {
                    if (symbolCount % 2 == 0) {
                    buttons[i].setIcon(new ImageIcon("images.jpg"));
                  //  buttons[i].setText("X");
                    validate();
                    } else {
                        buttons[i].setText("O");
                        buttons[i].setEnabled(false);
                    }

                }
            }
            if (count >= buttons.length) {
                JOptionPane.showMessageDialog(null, "End");
            }
            symbolCount++;
        }
    }
RKC
  • 1,834
  • 13
  • 13
flyiinhigh
  • 69
  • 1
  • 10
  • 1
    http://stackoverflow.com/questions/299495/how-to-add-an-image-to-a-jpanel same, only for JButton – Lukáš Rutar Apr 12 '14 at 19:42
  • @LukášRutar Not really, it's not about displaying an image in general (and you should NOT blindly override a JButton's `paintComponent` method...). @user3285009 Most likely, it does not find the image. What does `System.out.println(ImageIO.read(new File("images.jpg")));` print when you call this in the `main` method? What does `System.out.println(new File("images.jpg").exists());` print? Make sure that you are using the correct path, e.g. `...new ImageIcon("folder/images.jpg")` – Marco13 Apr 12 '14 at 20:51
  • Where is the image stored/located? – MadProgrammer Apr 12 '14 at 21:04

0 Answers0