2015-05-09 59 views
0

我正在做一個創建戰艦遊戲的項目。 我有以下類:將數據從GUI傳遞給Java中的另一個類

  • Battleship(主類)
  • Player
  • Game
  • Board
  • Visualisation(GUI類,其中我有一個的JButtons​​),其中用戶按下想要插入船隻的JButton

首先創建一個新的Game與一些參數,如板的大小。然後在課堂上Game我做了一個新的Visualisation。在這堂課內我已經完成了actionListeneractionPerformed

我的問題是如何傳遞的信息,例如,其中JButton我已經按下(插入在​​的單元格中的船)類Game?這是我有:

Class Game 
private Player _user; 
private Player _computer; 

然後,我要檢查的_user董事會如果該位置可插入船。 _user.MethodOfClassPlayer();

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

功能的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); 
    } 

我想通過將JButton的信息壓到遊戲類知道,如果該玩家該位置是否可用的把船。如果它是可用的,所以我會畫JButton 我希望你能理解我。

+0

我建議你通過戰艦類在GUI的構造,並保持對它的引用內勾起來作爲GUI名爲parent的一個字段:GUI(Battleship parent)和this.parent = parent。當單擊一個按鈕時,您可以通過執行main類中的函數來執行該函數:parent.myfunction() – Dien

+0

您可以在構造函數中使用參數傳遞某些數據,也可以使Visualization類成爲一個帶有返回值的對話框函數,當你點擊一個按鈕時,它將一個數組中的「x和y」保存在一個數組中,並將它返回到它在被處理後調用的主類中 – DoubleMa

+0

你能澄清你的意思嗎?這樣。你有沒有連接到按鈕的動作?你可以添加該代碼嗎? – ChiefTwoPencils

回答

-2
Class Game 
    private Player _user; 
    private Player _computer; 
    private Visualisation _vis=null; 

    /*_vis = new Visualisation(this) // in the constructor of Game*/ 


Class Visualisation 
     Game parent=null; 

    Visualisation(Game parent) 
    { 
     this.parent=parent; 
    } 

    void agivenfunction() 
    { 
     parent.myfunc(); 
    } 
+0

謝謝兄弟! 這工作:) :) – user3671361

+0

不客氣。做這樣的事情沒有什麼壞處,因爲你只是保留對象的引用。 – Dien

0

你可以使用一個對話框類是這樣的:

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

,並調用它在遊戲類應用:

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

考慮到另一個答案是y ou可能想要使用ObserversObservable。觀察者可以被一個或幾個觀察者觀察到。觀察者可以觀察幾個觀察者。

例子:

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) { 
       // ... 
      } 
     } 
    } 
} 

這裏是你如何一例的主要功能

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

謝謝你的回答,但我看不到與我的項目有關係。所以JButton必須是可觀察的?對不起,我的無知 – user3671361

+0

不是JButtons,而是它們所在的類。因爲你想擴展的類已經擴展了'JFrame',所以你必須創建一個內部類,它將'Observable'擴展到擴展'JFrame'的類中。然後在你的JFrame類中,你可以調用你的內部類的方法。希望你能理解。 – Sw0ut

+0

查看編輯我的答案 – Sw0ut