Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.IOException;
- public class GUIWindow extends JFrame {
- //menu container variables
- private JLabel title;
- private JLabel howToPlay;
- private JTextArea instructions;
- private JButton start;
- private JButton highScores;
- private JButton quit;
- private JPanel startScreen;
- private Container menuContainer;
- //high score container variables
- private JLabel titleScores;
- private JTextArea scoreList;
- private JButton backToMenu;
- private JPanel scoresScreen;
- private Container scoresContainer;
- //Constructor to set up the GUI
- public GUIWindow() {
- //initialize variables
- title = new JLabel("STACKER");
- title.setFont(new Font("SansSerif", Font.PLAIN, 50)); //setFont changes the JLabel's font, style, and size
- title.setHorizontalAlignment(JLabel.CENTER); //this will center the text horizontally
- start = new JButton("START");
- highScores = new JButton("SCORES");
- quit = new JButton("QUIT");
- howToPlay = new JLabel("How To Play");
- howToPlay.setFont(new Font("SansSerif", Font.BOLD, 25));
- howToPlay.setHorizontalAlignment(JLabel.CENTER);
- instructions = new JTextArea("Stack blocks by pressing the \n<space> key. Score increases \nby 1 point after"
- + " each successful \nstack. The more blocks you stack, \nthe harder the game will become. \nView your scores"
- + " by pressing \nthe \"Scores\" button.");
- instructions.setFont(new Font("SansSerif", Font.PLAIN, 15));
- startScreen = new JPanel();
- startScreen.add(title);
- startScreen.add(start);
- startScreen.add(highScores);
- startScreen.add(quit);
- startScreen.add(howToPlay);
- startScreen.add(instructions);
- menuContainer = getContentPane();
- menuContainer.add(startScreen, BorderLayout.CENTER);
- //Adds listeners to buttons, listens for a mouse click
- start.addActionListener(new StackerListener());
- highScores.addActionListener(new StackerListener());
- quit.addActionListener(new StackerListener());
- }
- private class StackerListener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- //getSource() checks to see which button is clicked, then performs the actions specific to that button
- if(e.getSource() == start) {
- }
- else if(e.getSource() == highScores) {
- String scoresString = "";
- //this try/catch was auto suggested, i did not write it myself
- try {
- Scores checkScores = new Scores();
- scoresString = checkScores.toString();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- titleScores = new JLabel("High Scores");
- titleScores.setFont(new Font("SansSerif", Font.PLAIN, 20));
- titleScores.setHorizontalAlignment(JLabel.CENTER);
- scoreList = new JTextArea(scoresString);
- scoreList.setFont(new Font("SansSerif", Font.PLAIN, 10));
- backToMenu = new JButton("Menu");
- scoresScreen = new JPanel();
- scoresScreen.add(titleScores);
- scoresScreen.add(scoreList);
- scoresScreen.add(backToMenu);
- scoresContainer = getContentPane();
- scoresContainer.add(scoresScreen, BorderLayout.CENTER);
- scoresContainer.validate();
- scoresContainer.repaint();
- backToMenu.addActionListener(new ScoresListener());
- }
- else {
- System.exit(1); //quits the program
- }
- }
- }
- private class ScoresListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- scoresContainer.setVisible(false);
- menuContainer.setVisible(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement