简体   繁体   English

JButton子级不更新文本

[英]JButton child does not update text

I'm building a game where the object is to move a knight around a world and be able to fight other knights. 我正在开发一款游戏,目标是在世界上移动一个骑士并能够与其他骑士战斗。

I've got a class Main that starts the game: 我有一个Main类开始游戏:

public class Main {

    public static void main(String[] args) {    
    new Game();
    }
}

A class Game that creates a JFrame: 创建JFrame的类Game:

import java.awt.GridLayout;

import javax.swing.JFrame;

public class Game {

    public Game() {
        JFrame frame = new JFrame();
        frame.setTitle("Knights Tournament");
        frame.add(new Board());
        frame.setSize(700, 700);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

And a class Board which (you guessed it!) creates the GameBoard: 还有一个(您猜对了!)创建GameBoard的类Board:

public class Board extends JPanel {

    Tile[][] grid = new Tile[15][15];

    public Board(){

        // Create the grid
        CreateGrid();

        // Add the player
        grid[0][0] = new Player(0, 0);
    }

    // Method that creates a grid of tiles
    private void CreateGrid() {
        setLayout(new GridLayout (15, 15));
        for(int i = 0; i < 15; i++){
            for(int j = 0; j < 15; j++){
                grid[i][j] = new Tile(i, j);
                add(grid[i][j]);
            }
        }
   }
}

The grid initialy consists of 15x15 tiles. 网格初始包含15x15的图块。

 public class Tile extends JButton implements ActionListener {

    public int xCo;
    public int yCo;

    public Tile(int x, int y) {
        setXCo(x);
        setYCo(y);
    }

    public void setXCo(int x) {
        this.xCo = x;
    }

    public void setYCo(int y) {
        this.yCo = y;
    }

    public int getXCo() {
        return xCo;
    }

    public int getYCo() {
        return yCo;
    }
}

The problem i'm facing is the following: I would like to replace grid[0][0] by another class Player that expands tile. 我面临的问题如下:我想用另一个扩展tile的类Player替换grid [0] [0]。 The difference between tile and player would be that the Jbutton gets a text saying 'P' i've tried this: tile和player之间的区别是,Jbutton收到了一个说“ P”的文本,我已经尝试过了:

public class Player extends Tile{

    public Player(int x, int y) {
        super(x, y);
        this.setText("P");
   }

}

In the constructor of the class board i tried changing grid[0][0] from tile to player so it would display P, but it doesn't do so for some reason (It does change the type of grid[0][0] to player...) I hope someone can help. 在类板的构造函数中,我尝试将grid [0] [0]从图块更改为播放器,以便显示P,但是由于某些原因它没有这样做(它确实更改了grid [0] [0]的类型]给玩家...)我希望有人可以提供帮助。

You shouldn't try and maintain the state of the object with the UI, per se . 本质上 ,您不应该尝试使用UI维护对象的状态。 Rather the UI should visualise the state of the underlying logic of the game (or model). 而是,UI应该可视化游戏(或模型)的基本逻辑的状态。 This allows you to modify the model, change the rules, add/remove elements (think adding new monsters or loot for example), without need to physically modify the UI or how it visualise this state (to a large degree). 这使您可以修改模型,更改规则,添加/删除元素(例如,考虑添加新的怪物或战利品),而无需实际修改UI或它如何可视化此状态(在很大程度上)。

The idea is, the Player attributes should be maintain with an class of it's own, which is managed by the model. 这个想法是, Player属性应使用自己的类维护,该类由模型管理。 The UI would then be used to visualise the state the model. 然后,UI将用于可视化模型的状态。

When clicked the Tile would notify the "controller". 单击时, Tile将通知“控制器”。 The controller would request stateful information from the games "model" and based on this information the "controller" would change the state of the button(s) to meet the requirements of the "model" 控制器将从游戏“模型”中请求状态信息,并且基于此信息,“控制器”将更改按钮的状态以满足“模型”的要求

This would simply require the "controller" to update the button(s) information (text/icons) without needing to change the physical buttons. 这仅需要“控制器”来更新按钮信息(文本/图标),而无需更改物理按钮。

This separates the responsibilities into defined layers and reduces the coupling of the code, so one or more layers can change, but the overall structure doesn't break down or need to be heavily modified to handle these changes. 这将职责划分为定义的层,并减少了代码的耦合,因此可以更改一个或多个层,但是整体结构不会崩溃或需要进行重大修改以处理这些更改。

The model is responsible for maintaining the virtual state of the game, for managing the interactions between the individual game objects (combat etc). 该模型负责维护游戏的虚拟状态,管理各个游戏对象之间的交互(战斗等)。

The UI is responsible for providing a visualisation of the model for the user and the controller is used to manage the interaction between the take, taking user input and pass it to the model, monitoring changes to the model's state and telling the UI when it needs to update. UI负责为用户提供模型的可视化,并且控制器用于管理镜头之间的交互,获取用户输入并将其传递给模型,监视模型状态的变化并在需要时告知UI更新。

The Model–view–controller paradigm is a common approach used in GUI development across different lanuguages. 模型-视图-控制器范例是在跨不同语言的GUI开发中使用的一种通用方法。 Take a look at Model–view–controller for more details. 查看Model-view-controller以获得更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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