You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java游戏开发:MyFrame无法调用Main中Player对象的AddXP()方法

Java游戏开发:解决跨类调用Player对象方法的问题

我正在用Java制作一款游戏,当前涉及Main、MyFrame和Player三个类。我的需求是:在Main类中初始化_player1对象后,在MyFrame类的JButton点击时调用该对象的AddXP()方法,但目前MyFrame无法识别Main中的_player1对象,也无法调用对应的方法。

Player类代码

public class Player {

    double FIVE_PERCENT = .05;
    double TEN_PERCENT = .1;

    double maxHP;
    double currHP;
    double maxStam;
    double currStam;
    double stamRegen = (TEN_PERCENT * maxStam);
    double hpRegen = TEN_PERCENT * maxHP;

    int xpToLvl;
    int currXP;
    int currLvl;

    Player(double _maxHP, double _currHP, double _maxStam, double _currStam, int _xpToLvl, int _currXP, int _currLvl) {
        maxHP = _maxHP;
        currHP = _currHP;
        maxStam = _maxStam;
        currStam = _currStam;
        xpToLvl = _xpToLvl;
        currXP = _currXP;
        currLvl = _currLvl;
    }

    public double RegenHP() {
        while(currHP < maxHP){
            try {
                currHP += maxHP * TEN_PERCENT;
                if (currHP > maxHP) {currHP = maxHP;}
                System.out.println(currHP + ": Health Points");

                Thread.sleep(500);

            } catch (Exception e) {
                System.out.println("uh oh stinky");
            }
        }

        return currHP;
    }


    public double RegenStam() {
        while(currStam < maxStam){
            try {
                currStam += maxStam * TEN_PERCENT;
                if (currStam > maxStam) {currStam = maxStam;}
                System.out.println(currStam + ": Stamina Points");

                Thread.sleep(500);

            } catch (Exception e) {
                System.out.println("uh oh stinky");
            }
        }

        return currStam;
    }

    public int CheckLevelUp() {
        if(currXP >= xpToLvl){
            xpToLvl *= 1.2;
            currLvl++;
            System.out.println("Current Level: " + currLvl);
            System.out.println("XP to next level: " + xpToLvl);
        }
        return currLvl;
    }

    public int AddXP() {
        System.out.println("Initial xp: " + currXP);
        currXP++;
        System.out.println("Post xp: " + currXP);

        CheckLevelUp();

        return currXP;
    }
}

Main类代码

public class Main {
    public static void main(String[] args) {
        new MyFrame();

        Player _player1 = new Player(40, 40, 50, 50, 24, 23, 1);

        _player1.RegenStam();
        _player1.RegenHP();
        _player1.AddXP();
    }
}

MyFrame类代码

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame{

        Player _player1;

        JButton xpButton;

        int SCREEN_WIDTH = 800;
        int SCREEN_HEIGHT = 600;

        MyFrame(){

            xpButton = new JButton();
            xpButton.setBounds( 0, 0, 20, 20); 
            xpButton.setVisible(true);
            xpButton.addActionListener((e) -> _player1.AddXP());

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            this.getContentPane().setBackground(new Color(200, 200, 215));
            this.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            this.add(xpButton);

        }
}

解决方案

问题核心是MyFrame中的_player1变量未被赋值,Main里创建的_player1是局部变量,无法被MyFrame直接访问。解决步骤如下:

  1. 修改MyFrame的构造方法,接收Player对象作为参数
    在MyFrame中添加带参数的构造方法,将传入的Player对象赋值给类内的_player1变量:

    public MyFrame(Player player) {
        this._player1 = player;
        // 保留原构造方法的初始化代码
        xpButton = new JButton();
        xpButton.setBounds(0, 0, 20, 20); 
        xpButton.setVisible(true);
        xpButton.addActionListener((e) -> _player1.AddXP());
    
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.getContentPane().setBackground(new Color(200, 200, 215));
        this.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        this.add(xpButton);
    }
    
  2. 调整Main类的对象创建顺序
    先创建_player1对象,再将它传入MyFrame的构造方法,确保MyFrame拿到正确的Player实例:

    public class Main {
        public static void main(String[] args) {
            Player _player1 = new Player(40, 40, 50, 50, 24, 23, 1);
            new MyFrame(_player1); // 将_player1传入MyFrame
    
            _player1.RegenStam();
            _player1.RegenHP();
            _player1.AddXP();
        }
    }
    
  3. 额外注意事项
    你的RegenHP()和RegenStam()方法使用了Thread.sleep(500),这会阻塞Swing的事件调度线程(EDT),导致界面卡顿。建议使用javax.swing.Timer来实现周期性的回复逻辑,避免阻塞UI线程。

内容的提问来源于stack exchange,提问作者Red-6969

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 07:31:16