Java Reference
In-Depth Information
JMenuBar menuBar = new JMenuBar();
menuBar.add(file);
frame.setJMenuBar(menuBar);
}
// Setup and display the user interface
private void createAndShowGUI() {
shootingGalleryPanel = new ShootingGalleryPanel();
addMenu(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
shootingGalleryPanel.setPreferredSize(gameDimension);
scorePanel.setPreferredSize(new Dimension(400, 50));
scorePanel.setLayout(new BoxLayout(scorePanel, BoxLayout.X_AXIS));
scorePanel.add(scoreLabel);
scorePanel.add(scoreDisplayLabel);
frame.getContentPane().add(scorePanel);
frame.getContentPane().add(shootingGalleryPanel);
frame.getContentPane().setLayout(new
BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
// The main method
public static void main(String[] args) {
ShootingGallery shootingGallery = new ShootingGallery();
shootingGallery.createAndShowGUI();
}
// Listen for the user choosing Exit from the menu
//
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() != null) {
if (e.getActionCommand().equals("Exit")) {
System.exit(0);
}
}
}
}
As usual, the ShootingGallery class just sets up the window and handles clicks to the menu and the
window's controls (minimize and close, in this case). I disabled the resize control on the window, both
because it simplified the math in the game and because it's something you might want to know how to
do someday. The following line removes the ability to resize the window:
Listing 12-5. Preventing Resizing
frame.setResizable(false);
Search WWH ::




Custom Search