2016-12-04 11 views
-2

私はいくつかのボタンを使ってJavaで推測グリッドゲームを作っています。プログラムはJavaのgridlayoutレイアウトを使用し、ボタンをクリックすると配列にGridLayoutの位置(x &ではなく)を追加します。ここでJavaでJButtonのgridlayoutセルの位置を取得する方法は?

は、セル位置ファインダーが必要とされるコードのストリップです。

public void actionPerformed(ActionEvent e) 
    { 


     if(placementLimit < 5){ 

     //placementlimit is limit to buttons clicked 

     JButton clickedButton = (JButton)e.getSource(); 

     clickedButton.setBackground(Color.RED); 
     /* something like: int pos = clickedButton.getGridPos 
     * arrayplacedpositions.add(pos); 
     */ 
     placementLimit++; 
    } 

私はそう プロジェクト全体がここにあるのJPanelをしなかった:

 /* Guessing game GoldMiner By Alexander Smirnov 
* A Computer Game where you hide and place gold. 
*/ 
package standard; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridLayout; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.util.ArrayList; 
import java.util.Arrays; 


import javax.swing.*; 

public class game extends JPanel 
{ 
    JButton buttons[] = new JButton[64]; 
    int placementLimit = 0; 
    ArrayList goldPos = new ArrayList(); 


    public game() 
    { 
     setLayout(new GridLayout(8,8)); 
     initializebuttons(); 
    } 


    public void initializebuttons() 
    { 
     for(int i = 0; i <= 63; i++) 
     { 
      buttons[i] = new JButton(); 
      buttons[i].setText(""); 
      buttons[i].addActionListener(new buttonListener()); 

      add(buttons[i]); 
     } 
    } 



    private class buttonListener implements ActionListener 
    { 

     public void actionPerformed(ActionEvent e) 
     { 


      if(placementLimit < 5){ 
      JButton clickedButton = (JButton)e.getSource(); 
      clickedButton.setIcon(new ImageIcon("/Users/Administrator/Desktop/gold2.jpg")); 
      clickedButton.setBackground(Color.RED); 
      JPanel.getComponents(); 
      placementLimit++; 
     } else { 


      JOptionPane.showMessageDialog(null, "You have placed the maximum amount of gold!"); 

      } 

      } 






    } 
    public static void main(String[] args) 
    { 
     JFrame window = new JFrame("GoldMiner"); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.getContentPane().add(new game()); 
     window.setBounds(300,200,300,300); 
     Container c = window.getContentPane(); 

     window.setVisible(true); 
    } 
} 
+0

これが重複していない場合は、あなたの修正されたアプローチを示している[MCVE]が含まれるようにあなたの質問を編集してください。 – trashgod

答えて

2

それが一番簡単な方法はありません...多分であなたのJButton Sを保持するために、より有用であろう2D配列を使用するか、JButtonのマップを適切に配置します。あなたは本当にボタンからグリッドセルを識別しなければならない場合

JPanel.getComponents()(または親コンテナが何であれ)を呼び出し、そこにあなたのボタンを見つけます。それから数学をする。

あなたは、インデックス6.あなたのグリッドが行を取得するためのxによるyの除算することにより、X、及びmodは列を取得するにはxであるので、次に

でそれを見つけると言います。

よう

6%4が2であるので、あなたが持つ列にある索引1(第2列)と行にあるようにグリッドIX 4x4の

6/4は、1であると言いますインデックス2(3列目)

+0

コード例を挙げてください。 –

+0

また、私のボタンはforループを使って形成されているので、手動で各ボタンを呼び出すことはできません。良いアドバイスのため –

+0

(1+)。また、もしそれがkludgeであるかどうかは分かりませんが、 'putClientProperty(...)'メソッドを使ってグリッド位置をJButtonに埋め込むことができます。 –

関連する問題