2014-01-29 59 views
0

我正在嘗試使用Java Swing編寫一個Battleship程序,目前我有一個使兩個網格的類。我試圖找出哪個按鈕被點擊的位置,所以我可以稍後使用它來放置鏡頭等等。不幸的是,我在這方面遇到了一些麻煩。Java Swing ActionListener顯示JButton數組

我已經得到了使用actionPerformed方法打印出來的所有東西的對象,但我只想要grid [x] [y]。我如何去做這件事?

在此先感謝您的幫助。

package testapp; 

/** 
* 
* @author Craig 
*/ 
import javax.swing.JFrame; 
import javax.swing.*; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.border.Border; 

public class menu extends JPanel implements ActionListener{ 
     JButton[][] grid; 
     TextField text = new TextField(20); 

    public menu(int width, int length) { 


     Border playerBorder = BorderFactory.createTitledBorder("Player"); 
     Border comBorder = BorderFactory.createTitledBorder("Com"); 


     JPanel player = new JPanel(); 
     player.setBorder(playerBorder);// set border round player grid 
     player.setLayout(new GridLayout(4,4)); 

     grid=new JButton[width][length]; //allocate the size of grid 
     for(int y=0; y<length; y++){ 
      for(int x=0; x<width; x++){ 
       grid[x][y]=new JButton(); //creates new button  
       player.add(grid[x][y]); //adds button to grid 
       grid[x][y].setBackground(Color.BLUE);//sets grid background colour 
       grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions 
       add(text); 
       grid[x][y].addActionListener(this); 
      } 
     } 

     JPanel com = new JPanel(); 
     com.setBorder(comBorder);// set border round com grid   
     com.setLayout(new GridLayout(4,4)); 
     grid=new JButton[width][length]; //allocate the size of grid 
     for(int y=0; y<length; y++){ 
      for(int x=0; x<width; x++){ 
       grid[x][y]=new JButton(); //creates new button 
       com.add(grid[x][y]); //adds button to grid 
       grid[x][y].setBackground(Color.BLUE);//sets grid background colour 
       grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions 
      } 
     }   

     //this.setLayout(new FlowLayout()); 
     this.add(player); 
     this.add(com); 

} 
    public void actionPerformed(ActionEvent e) { 
     Object source = e.getSource(); 
     if (source instanceof JButton) { 
      JButton btn = (JButton)source; 
      text.setText("IN THE BOX "); 
     } 

    } 

} 
+0

通過網格就循環就像你加入他們,並檢查電網[X] [Y]按鈕=='source' –

+1

[這個問題](http://stackoverflow.com/q/21346281/877472)幾天前出現,我認爲這與你的情況非常相似(如果不是確切的話)。關於OP的解決方案是否合適存在一些爭議,但Trashgod的答案有很多很好的信息。最終,OP在其問題中的解決方案可能是解決問題的一種方法。 –

+0

謝謝你會看看 – user3249467

回答

1

有多種選擇。如果擴展JButton,恕我直言是最後的手段,而且幾乎沒有必要。循環遍歷grid [] []數組並檢查它們是否爲source的各個事件可能沒問題。但另一個(恕我直言,簡單而優雅的)解決方案是使用匿名聽衆:

// Where the grid buttons are created: 
.... 
grid[x][y].addActionListener(createActionListener(x, y)); 


private ActionListener createActionListener(final int x, final int y) 
{ 
    return new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      clickedButton(x, y); 
     } 
    }; 
} 

private void clickedButton(int x, int y) 
{ 
    System.out.println("Clicked "+x+" "+y); 
} 

編輯:再次,在形式的http://sscce.org/(你可能已經創建了一個,那麼我可以集成在我的例子答案.. 。)

/** 
* 
* @author Craig and me :D 
*/ 
import javax.swing.JFrame; 
import javax.swing.*; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.border.Border; 

public class menu extends JPanel { 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(new menu(4,4)); 
     f.setLocationRelativeTo(null); 
     f.pack(); 
     f.setVisible(true); 
    } 

    JButton[][] grid; 
    TextField text = new TextField(20); 

    public menu(int width, int length) { 


     Border playerBorder = BorderFactory.createTitledBorder("Player"); 
     Border comBorder = BorderFactory.createTitledBorder("Com"); 


     JPanel player = new JPanel(); 
     player.setBorder(playerBorder);// set border round player grid 
     player.setLayout(new GridLayout(4,4)); 

     grid=new JButton[width][length]; //allocate the size of grid 
     for(int y=0; y<length; y++){ 
      for(int x=0; x<width; x++){ 
       grid[x][y]=new JButton(); //creates new button  
       player.add(grid[x][y]); //adds button to grid 
       grid[x][y].setBackground(Color.BLUE);//sets grid background colour 
       grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions 
       add(text); 
       grid[x][y].addActionListener(
        createActionListener(x, y, "Player")); 
      } 
     } 

     JPanel com = new JPanel(); 
     com.setBorder(comBorder);// set border round com grid   
     com.setLayout(new GridLayout(4,4)); 
     grid=new JButton[width][length]; //allocate the size of grid 
     for(int y=0; y<length; y++){ 
      for(int x=0; x<width; x++){ 
       grid[x][y]=new JButton(); //creates new button 
       com.add(grid[x][y]); //adds button to grid 
       grid[x][y].setBackground(Color.BLUE);//sets grid background colour 
       grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions 
       grid[x][y].addActionListener(
        createActionListener(x, y, "Computer")); 
      } 
     }   

     //this.setLayout(new FlowLayout()); 
     this.add(player); 
     this.add(com); 

    } 
    private ActionListener createActionListener(
     final int x, final int y, final String name) 
    { 
     return new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       clickedButton(x, y, name); 
      } 
     }; 
    } 

    private void clickedButton(int x, int y, String name) 
    { 
     System.out.println("Clicked "+x+" "+y+" for "+name); 
    } 
} 
+0

嗨,我將如何去實現這一點,因爲我現在得到的錯誤,如:在我得到的類名[菜單不抽象,不重寫抽象方法],我在哪裏創建方法私人ActionListener我得到[非法表達的開始]。對不起,如果這些是非常簡單的問題,只是卡住 – user3249467

+0

添加了一個包含http://sscce.org/ – Marco13

+0

的編輯對不起,我沒有意識到(第一次發佈到論壇)。 – user3249467