Advertisement
Guest User

GUIWindow

a guest
Jan 14th, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.IOException;
  5.  
  6. public class GUIWindow extends JFrame {
  7.     //menu container variables
  8.     private JLabel title;
  9.     private JLabel howToPlay;
  10.     private JTextArea instructions;
  11.     private JButton start;
  12.     private JButton highScores;
  13.     private JButton quit;
  14.     private JPanel startScreen;
  15.     private Container menuContainer;
  16.    
  17.     //high score container variables
  18.     private JLabel titleScores;
  19.     private JTextArea scoreList;
  20.     private JButton backToMenu;
  21.     private JPanel scoresScreen;
  22.     private Container scoresContainer;
  23.    
  24.    
  25.     //Constructor to set up the GUI
  26.     public GUIWindow() {
  27.        
  28.         //initialize variables
  29.         title = new JLabel("STACKER");
  30.         title.setFont(new Font("SansSerif", Font.PLAIN, 50)); //setFont changes the JLabel's font, style, and size
  31.         title.setHorizontalAlignment(JLabel.CENTER); //this will center the text horizontally
  32.         start = new JButton("START");
  33.         highScores = new JButton("SCORES");
  34.         quit = new JButton("QUIT");
  35.         howToPlay = new JLabel("How To Play");
  36.         howToPlay.setFont(new Font("SansSerif", Font.BOLD, 25));
  37.         howToPlay.setHorizontalAlignment(JLabel.CENTER);
  38.         instructions = new JTextArea("Stack blocks by pressing the \n<space> key. Score increases \nby 1 point after"
  39.                 + " each successful \nstack. The more blocks you stack, \nthe harder the game will become. \nView your scores"
  40.                 + " by pressing \nthe \"Scores\" button.");
  41.         instructions.setFont(new Font("SansSerif", Font.PLAIN, 15));
  42.        
  43.         startScreen = new JPanel();
  44.         startScreen.add(title);
  45.         startScreen.add(start);
  46.         startScreen.add(highScores);
  47.         startScreen.add(quit);
  48.         startScreen.add(howToPlay);
  49.         startScreen.add(instructions);
  50.         menuContainer = getContentPane();
  51.         menuContainer.add(startScreen, BorderLayout.CENTER);
  52.        
  53.        
  54.         //Adds listeners to buttons, listens for a mouse click
  55.         start.addActionListener(new StackerListener());
  56.         highScores.addActionListener(new StackerListener());
  57.         quit.addActionListener(new StackerListener());
  58.        
  59.     }
  60.    
  61.     private class StackerListener implements ActionListener{
  62.         public void actionPerformed(ActionEvent e) {
  63.             //getSource() checks to see which button is clicked, then performs the actions specific to that button
  64.             if(e.getSource() == start) {
  65.                
  66.             }
  67.             else if(e.getSource() == highScores) {
  68.                 String scoresString = "";
  69.                
  70.                 //this try/catch was auto suggested, i did not write it myself
  71.                 try {
  72.                     Scores checkScores = new Scores();
  73.                     scoresString = checkScores.toString();
  74.                    
  75.                 } catch (IOException e1) {
  76.                     e1.printStackTrace();
  77.                 }
  78.                 titleScores = new JLabel("High Scores");
  79.                 titleScores.setFont(new Font("SansSerif", Font.PLAIN, 20));
  80.                 titleScores.setHorizontalAlignment(JLabel.CENTER);
  81.                 scoreList = new JTextArea(scoresString);
  82.                 scoreList.setFont(new Font("SansSerif", Font.PLAIN, 10));
  83.                 backToMenu = new JButton("Menu");
  84.                 scoresScreen = new JPanel();
  85.                 scoresScreen.add(titleScores);
  86.                 scoresScreen.add(scoreList);
  87.                 scoresScreen.add(backToMenu);
  88.                 scoresContainer = getContentPane();
  89.                 scoresContainer.add(scoresScreen, BorderLayout.CENTER);
  90.                 scoresContainer.validate();
  91.                 scoresContainer.repaint();
  92.                 backToMenu.addActionListener(new ScoresListener());
  93.             }
  94.             else {
  95.                 System.exit(1); //quits the program
  96.             }
  97.         }
  98.     }
  99.     private class ScoresListener implements ActionListener {
  100.         public void actionPerformed(ActionEvent e) {
  101.             scoresContainer.setVisible(false);
  102.             menuContainer.setVisible(true);
  103.         }
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement