Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 7 ■ WRITING A USER INTERFACE

private void addAndArrangePanels(JFrame frame) {
Border paddingBorder = BorderFactory.createEmptyBorder(5,5,5,5);
JPanel controlPanel = new JPanel();
minesFoundLabel.setBorder(paddingBorder);
minesFoundLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
minesRemainingLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
minesRemainingLabel.setBorder(paddingBorder);
minesRemainingLabel.setText("Remaining: " + mineField.getMinesRemaining());
controlPanel.add(minesFoundLabel);
controlPanel.add(minesRemainingLabel);
GridLayout gridLayout = new GridLayout(1,2);
controlPanel.setLayout(gridLayout);
frame.getContentPane().add(controlPanel);
frame.getContentPane().add(minePanel);
}


private void addMenu(JFrame frame) {
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New Game");
newItem.setMnemonic('n');
newItem.addActionListener(actionListener);
file.add(newItem);
ButtonGroup sizeOptions = new ButtonGroup();
JRadioButtonMenuItem smallOption = new JRadioButtonMenuItem("Small (8 x 8, 10 mines)");
smallOption.setMnemonic('s');
smallOption.addActionListener(actionListener);
sizeOptions.add(smallOption);
file.add(smallOption);
JRadioButtonMenuItem mediumOption =
new JRadioButtonMenuItem("Medium (16 x 16, 40 mines)");
mediumOption.setMnemonic('m');
mediumOption.addActionListener(actionListener);
sizeOptions.add(mediumOption);
file.add(mediumOption);
JRadioButtonMenuItem largeOption =
new JRadioButtonMenuItem("Large (16 x 32, 100 mines)");
largeOption.setMnemonic('l');
largeOption.addActionListener(actionListener);
sizeOptions.add(largeOption);
file.add(largeOption);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
exitItem.addActionListener(actionListener);
file.add(exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(file);
frame.setJMenuBar(menuBar);
}

Free download pdf