简体   繁体   中英

Java method call from inside class different output than outside class

I am making a Tic-Tac-Toe computer and I need to know when the current letter is O so the computer can make its move. However, from the main method, the current letter always looks like O , but from inside the GUI class the current letter changes as it should.

Here is the GUI class: I tried to only include the relevant methods.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class TicGUI extends JPanel {    
    private JButton btns[] = new JButton[9];
    private String xnos[] = new String[9];
    private String currentLetter = "X";

    public TicGUI() {
        initBtns();
        initBoard();
    }

    public String getCurrentLetter() {
        return currentLetter;
    }

    public void initBtns() {
        for(int i = 0; i < 9; i++) {
            btns[i] = new JButton();
            btns[i].addActionListener(new TicListener());
        }
    }

    public void initBoard() {
        this.setLayout(new GridLayout(3, 3));
        for(int i = 0; i < btns.length; i++) {
            this.add(btns[i]);
        }
    }

    private class TicListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton) e.getSource();
            if(btn.getText().equals("") && btn.isEnabled()){
                btn.setText(currentLetter);
                btn.setEnabled(false);
                if(currentLetter.equals("X"))
                    currentLetter = "O";
                else
                    currentLetter = "X";
                System.out.println("In class:" + getCurrentLetter());
            }
        }
    }
}

Here is the class with the main method:

import java.util.Scanner;

import javax.swing.JFrame;

public class TICTICTIC {

    public static void main(String[] args) {
        TicGUI tic = new TicGUI();
        JFrame frame = new JFrame("t");
        frame.add(new TicGUI());
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        Scanner scan = new Scanner(System.in);
        while(true) {
            String ans = scan.nextLine();
            System.out.println(tic.getCurrentLetter());
        }
    }
}

My expected output would be:

In main: X         // <- pressed enter
In GUI class: O    // <- clicked a button, made currentLetter change

In main: O         // <- pressed enter
In GUI class: X    // <- clicked a button, made currentLetter change

In main: X         // <- you get the idea
In GUI class: O    

In main: O

The "In main" letter alternates along with the GUI class output.

Instead I get:

In main: X
In GUI class: O

In main: X
In GUI class: X

In main: X
In GUI class: O

In main: X

The "In main" output stays the same the whole time. Why?

You're creating two instances of TicGUI ...

TicGUI tic = new TicGUI();
JFrame frame = new JFrame("t");
frame.add(new TicGUI());

One is getting added to the GUI the other you are trying to extract the value from, each has it's own value of currentLetter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM