2016-03-26 13 views
0

'daki düğmelerin 2 dizisinden hangi tuşa basıldığını nasıl söylerim İki adet JFartta gösterilen düğmelerim var ve hangisine basıldığını söylemem gerekiyor, Geçerli çözümüm yalnızca bir tuşa İlk pencerede basılan ilk pencere, ilk pencerede bir düğmeyi tıkladığınızda, program yeniden başlatılana kadar ikinci pencerelerde bir düğmeyi tıklayamayacaksınız.İki JFrames

Kodum

private void populateArray() 
{ 
    for(int wy = 0;wy<8;wy++) 
    { 
     for(int wx =0; wx<8;wx++) 
     { 
      WhiteButton[wx][wy] = new ReversiButton(); 
      WhiteButton[wx][wy].addActionListener(this); 
      if((wx == 3)&&(wy ==3)) 
       WhiteButton[wx][wy].change(1); 
      else if((wx == 4)&&(wy == 3)) 
       WhiteButton[wx][wy].change(2); 
      else if((wx == 3)&&(wy == 4)) 
       WhiteButton[wx][wy].change(2); 
      else if((wx ==4)&&(wy == 4)) 
       WhiteButton[wx][wy].change(1); 
     } 
    } 
    for(int by = 0; by<8; by++) 
    { 
     for(int bx =0; bx<8;bx++) 
     { 
      BlackButton[bx][by] = new ReversiButton(); 
      BlackButton[bx][by].addActionListener(this); 
      if((bx == 3)&&(by ==3)) 
       BlackButton[bx][by].change(1); 
      else if((bx == 4)&&(by == 3)) 
       BlackButton[bx][by].change(2); 
      else if((bx == 3)&&(by == 4)) 
       BlackButton[bx][by].change(2); 
      else if((bx ==4)&&(by == 4)) 
       BlackButton[bx][by].change(1); 
     } 
    } 
} 
private void initGUI() 
{ 
    WhiteFrame.setTitle("Reversi White Player"); 
    BlackFrame.setTitle("Reversi Black Player"); 
    WhiteFrame.setLayout(new BorderLayout()); 
    WhiteLabel.setText("White Player - click place to put piece"); 
    WhiteGrid.setLayout(new GridLayout(8,8)); 
    for(int wy = 0;wy<8;wy++) 
    { 
     for(int wx =0; wx<8;wx++) 
     { 
      WhiteGrid.add(WhiteButton[wx][wy]); 
     } 
    } 
    WhiteButtons.setText("Greedy AI(play white)"); 
    WhiteFrame.add(BorderLayout.NORTH,WhiteLabel); 
    WhiteFrame.add(BorderLayout.CENTER,WhiteGrid); 
    WhiteFrame.add(BorderLayout.SOUTH,WhiteButtons); 
    WhiteFrame.pack(); 
    WhiteFrame.setVisible(true); 
    BlackFrame.setLayout(new BorderLayout()); 
    BlackLabel.setText("Black player - not your turn"); 
    BlackGrid.setLayout(new GridLayout(8,8)); 
    BlackButton = Reverse.rotatearray(BlackButton); 
    for(int by = 0; by<8; by++) 
    { 
     for(int bx =0; bx<8;bx++) 
     { 
      BlackGrid.add(BlackButton[bx][by]); 
     } 
    } 
    BlackButtons.setText("Greedy AI(play black)"); 
    BlackFrame.add(BorderLayout.NORTH, BlackLabel); 
    BlackFrame.add(BorderLayout.CENTER, BlackGrid); 
    BlackFrame.add(BorderLayout.SOUTH,BlackButtons); 
    BlackFrame.pack(); 
    BlackFrame.setLocation(WhiteFrame.getX() + WhiteFrame.getWidth() + 10, WhiteFrame.getY()); 
    BlackFrame.setVisible(true); 
} 
@Override 
    public void actionPerformed(ActionEvent ae) 
    { 
     for (int y = 0; y < GRIDSIZE; y++) 
     { 
      for (int x = 0; x < GRIDSIZE; x++) 
      { 
       if((ae.getSource()==WhiteButton[x][y])) 
       { 
        if(WhiteButton[x][y].is() ==0) 
        { 
         WhiteButton[x][y].change(1); 
         Game.search(WhiteButton, x, y, 1); 
        } 
       } 
       if((ae.getSource() == BlackButton[x][y])) 
       { 
        if(BlackButton[x][y].is() == 0) 
        { 
         BlackButton[x][y].change(2); 
         Game.search(BlackButton, x, y, 2); 
        } 
       } 
      } 
     } 
    } 

Nasıl hem JFrames gelen düğmeye basılmasını algılayabilir mi?

WhiteButton[wx][wy].setActionCommand(w + wx + "," + wy); 

ve bu Dize almak için getActionCommand çağırır:

+0

Bkz. [Birden Fazla JFram Kullanılması, İyi/Kötü Uygulama?] (Http://stackoverflow.com/q/9554636/418556) –

cevap

3

Her düğme için bir eylem komutu tanımlayabiliriz. Alternatif olarak, bir Harita

nolu bağlantıyı yapın ve koordinatları ve geçerli durumu ButtonData sınıfındaki bir nesnede depolayın.

+0

Daha fazlası [here] (http://stackoverflow.com/a/7706684/ 230.513). – trashgod