简体   繁体   English

将数据从GUI传递到Java中的另一个类

[英]Passing data from GUI to another class in Java

I am doing a project that consist on creating a Battleship Game. 我正在做一个创建战舰游戏的项目。 I have the following classes: 我有以下课程:

  • Battleship (main class) Battleship (主舱)
  • Player , Player
  • Game , Game
  • Board , Board
  • Visualisation (GUI class where I have a gridLayout of JButtons ) where the user press the JButton where want to insert the ship. Visualisation (GUI类在那里我有一个gridLayoutJButtons在用户按下) JButton地方要插入的船。

First of all a create a new Game with some parameters such as the size of the board. 首先,创建一个带有一些参数(例如棋盘尺寸)的新Game Then inside the class Game I do a new Visualisation . 然后在类Game ,进行新的Visualisation Inside this class I have done the actionListener and the actionPerformed . 在此类中,我完成了actionListeneractionPerformed

My question is how I can pass the information, for example, of which JButton I have pressed (to insert the ship in that cell of the gridLayout ) to the class Game ? 我的问题是我如何将信息(例如,我按下的JButton (将飞船插入到gridLayout该单元格中))传递给Game类? This is what I have: 这就是我所拥有的:

Class Game
 private Player _user;
 private Player _computer;

And then I want to check the board of the _user if that positions are available to insert the ship. 然后,我想检查_user是否可以插入船。 _user.MethodOfClassPlayer();

Class Player
  private int id;
  private String name;
  private Board _boardPlayer
Class Board
  private int size;
  private int[][] _board = null ;

Function actionPerformed 执行的功能

  public void actionPerformed(ActionEvent e) {
    for ( int i = 0; i < tUsuariCPU.length; i++ ){
            for ( int j = 0; j < tUsuariCPU[i].length; j++ ){
                if ( e.getSource() == tUsuariCPU[i][j] ){
                        buttonPressedUser(i,j);
                        JButton temp = (JButton) e.getSource() ;
                        temp.setBackground(java.awt.Color.ORANGE);
    }

I want to pass the information of the JButton pressed to Game class to know if for that Player that position is available or not for putting the ship. 我想将按JButton的信息传递给Game类,以了解该玩家是否可以使用该位置放置飞船。 If it is available so I will paint the JButton I hope you understand me. 如果可用,那么我将绘制JButton,希望您理解我。

Taking account of the other answer, you may want to use Observers and Observable . 考虑到其他答案,您可能需要使用ObserversObservable An Observable is a class that can be observed by one or several Observers. 一个Observable是可以由一个或几个Observer观察到的类。 And an Observer can observe several Observables. 观察者可以观察几个观察对象。

Example: 例:

public class Visualisation extends JFrame {
    private Integer num;
    private A myInnerClass;

    public Visualisation(Observer o) {
        num = 8;
        myInnerClass = new A();
        myInnerClass.setObserver(o);
    }

    public onButtonPressed(Event e) {
        myInnerClass.notifyMyObservers();
    }

    public class A extends Observable {
        public A() {
        }

        public void notifyMyObservers() {
            this.setChanged();
            this.notifyObservers(num); // the parameter can be any object
        }
    }
}

public class B implements Observer {
    public B() {
    }

    public void update(Observable observed, Object arg) {
        if (observed instance of A) {
            if (arg instance of Integer) {
                // ... 
            }
        }
    }
}

And here is how you hook them up within an example main function 这是您如何将它们连接到示例主函数中的方法

public static main(String[] args) {
    B observer = new B();
    Visualisation v = new Visualisation(observer);
}

you can use a dialog class like this: 您可以使用如下对话框类:

public class Visualisation extends JDialog {

    private Board board;

    public Game(Frame owner, boolean modal) {
        super(owner, modal);

        // add a Listener to your button to dispose the dialog after you save your data in board
        JButton yourButton = new JButton();
        yourButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                board = ....;
                dispose();
            }
        });
    }

    public Board show(){
        setVisible(true);
        return Board ;
    }
}

and to call it in the game class use: 并在游戏类中调用它,请使用:

Visualisation visualisationDialog = new Visualisation(null, true);
Board board = visualisationDialog .show();
//do whatever you want with the board from here now

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

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