2014-01-29 56 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