2013-04-07 60 views
1

我用jbuttons制作了一个扫雷游戏,我不知道如何再次访问所有按钮,以便在输掉后显示最终的游戏。我通过使用Jbuttons中使用setText方法可以设置文本,但我不知道如何再试一次访问它们是我创造了所有的按钮,如果有帮助:揭示扫雷游戏中的所有空间

for(int x = 0; x < rows ;x++) 
    { 
     for(int j = 0; j < columns; j++) 
     { 
      MineButton button = new MineButton(x,j); 
      // adds a mouselistener for every button 
      button.addMouseListener(new MouseHandler()); 
      this.add(button); 
     } 
    } 

我可以把它变成一个数组但我认为我不能透过这些来透露他们。任何建议是受欢迎的。

+0

保持周围Jbuttons中的数组(或作为一个ArrayList另一种数据结构等),按照你的建议,那么你就可以遍历/他们枚举做任何你想要的操作。 – Patashu 2013-04-07 23:21:47

+0

'“我可以把它变成一组jbuttons,但我不认为我可以通过这些来揭示它们......” - 实际上这样做会工作并且可以很好地工作。 – 2013-04-07 23:22:08

+0

你会怎么去初始化呢?你会在数组的每个部分创建一个新按钮吗? – 2013-04-07 23:25:06

回答

3

您应该创建一些数组,它将存储对所有按钮的引用。请参阅我的小例子,这将有助于你了解它是如何工作的:

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class MineSweeper { 

    public static void main(String[] args) { 
     MainFrame window = new MainFrame(); 
     window.setVisible(true); 
    } 
} 

/** 
* Main frame. Initialize window and adds panel with buttons and clear button on 
* the window. 
* */ 
class MainFrame extends JFrame { 

    private static final long serialVersionUID = 1L; 

    private final Board board = new Board(10, 11); 

    public MainFrame() { 
     setLocation(400, 400); 
     setLayout(new GridLayout(2, 1)); 
     add(board); 
     add(createClearButton()); 
     pack(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private JButton createClearButton() { 
     JButton button = new JButton(); 
     button.setText("Clear"); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       board.clear(); 
      } 
     }); 
     return button; 
    } 
} 

/*** 
* This class contains all buttons on one panel. We initialize all buttons in 
* constructor. We can use {@link Board#clear()} method for reveal all buttons. 
* */ 
class Board extends JPanel { 

    private static final long serialVersionUID = 1L; 

    private JButton[][] plate; 
    private int numberOfRows; 
    private int numberOfColumns; 

    public Board(int numberOfRows, int numberOfColumns) { 
     this.numberOfRows = numberOfRows; 
     this.numberOfColumns = numberOfColumns; 
     this.plate = new JButton[numberOfColumns][numberOfRows]; 
     setLayout(new GridLayout(numberOfRows, numberOfColumns)); 
     init(); 
    } 

    private void init() { 
     for (int x = 0; x < numberOfColumns; x++) { 
      for (int y = 0; y < numberOfRows; y++) { 
       JButton button = createNewJButton(x, y); 
       plate[x][y] = button; 
       add(button); 
      } 
     } 
    } 

    private JButton createNewJButton(int x, int y) { 
     JButton button = new JButton(); 
     button.setText(x + ", " + y); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JButton button = (JButton) e.getSource(); 
       button.setText("X"); 
      } 
     }); 
     return button; 
    } 

    public void clear() { 
     for (int x = 0; x < numberOfColumns; x++) { 
      for (int y = 0; y < numberOfRows; y++) { 
       plate[x][y].setText(x + ", " + y); 
      } 
     } 
    } 
} 
+1

谢谢你的回答! – 2013-04-08 01:16:28

+1

@Hovercraft全食鳗鱼,好点。谢谢你的建议。我改变了它。新名称 - MainFrame。 – 2013-04-08 19:10:58