CHAPTER 7 ■ WRITING A USER INTERFACE
import javax.swing.JRadioButtonMenuItem;
import javax.swing.border.Border;
public class MineSweeper {
int columns = 8;
int rows = 8;
int numberOfMines = 10;
JFrame frame = new JFrame("MineSweeper");
JPanel minePanel = new JPanel();
JLabel minesRemainingLabel = new JLabel("Remaining:");
JLabel minesFoundLabel = new JLabel("Found: 0");
JButton[][] mineButtons;
Dimension buttonSize = new Dimension(20, 20);
MineField mineField;
MineSweeperMouseListener mouseListener;
MineSweeperActionListener actionListener;
MineSweeperHelper helper;
public MineSweeper() {
helper = new MineSweeperHelper(this);
actionListener = new MineSweeperActionListener(this, helper);
mouseListener = new MineSweeperMouseListener(this, helper);
init();
}
void init() {
mineButtons = new JButton[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
JButton currentButton = new JButton();
mineButtons[i][j] = currentButton;
currentButton.setPreferredSize(buttonSize);
currentButton.setMaximumSize(buttonSize);
currentButton.setMinimumSize(buttonSize);
currentButton.addMouseListener(mouseListener);
currentButton.setEnabled(true);
currentButton.setText("");
currentButton.setIcon(null);
}
}
minePanel.setLayout(new GridLayout(rows, columns));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
minePanel.add(mineButtons[i][j]);
}
}
mineField = new MineField(rows, columns, numberOfMines);
}