【问题标题】:Java method call from inside class different output than outside class来自类内部的 Java 方法调用与外部类不同的输出
【发布时间】:2018-07-21 00:22:27
【问题描述】:

我正在制作一台井字游戏计算机,我需要知道当前字母何时为O,以便计算机可以移动。但是,从 main 方法来看,当前字母总是看起来像 O,但从 GUI 类内部,当前字母会发生应有的变化。

这是 GUI 类:我尝试只包含相关方法。

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());
            }
        }
    }
}

这是带有 main 方法的类:

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());
        }
    }
}

我的预期输出是:

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

“In main”字母与 GUI 类输出交替出现。

相反,我得到:

In main: X
In GUI class: O

In main: X
In GUI class: X

In main: X
In GUI class: O

In main: X

“In main”输出始终保持不变。为什么?

【问题讨论】:

  • TicGUI.currentLetter 在单击按钮时更改。代码在TicListener。

标签: java swing methods


【解决方案1】:

您正在创建TicGUI...的两个实例...

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

一个被添加到 GUI 中,另一个你试图从中提取值,每个都有自己的值 currentLetter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 2021-04-30
    • 1970-01-01
    • 2012-02-21
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多