-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartingFrame.java
90 lines (72 loc) · 2.55 KB
/
StartingFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* this template can be used for a start menu
* for your final project
**/
//Imports
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class StartingFrame extends JFrame {
JFrame thisFrame;
// Constructor - this runs first
StartingFrame() {
super();
this.thisFrame = this; // lol
// configure the window
this.setSize(300, 200);
this.setLocationRelativeTo(null); // start the frame in the center of
// the screen
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
// Create a Panel for stuff
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
// Create a JButton for the centerPanel
JButton playButton = new JButton("Play");
playButton.addActionListener(new StartButtonListener());
JButton instructionButton = new JButton("How to play");
instructionButton.addActionListener(new InstructionButtonListener());
// Create a JButton for the centerPanel
JLabel startLabel = new JLabel("Toohs", SwingConstants.CENTER);
Font font = new Font("SansSerif", Font.BOLD, 24);
startLabel.setFont(font);
// Add all panels to the mainPanel according to border layout
playButton.setAlignmentX(Component.CENTER_ALIGNMENT);
instructionButton.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(playButton);
buttonPanel.add(instructionButton);
mainPanel.add(startLabel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
// add the main panel to the frame
this.add(mainPanel);
// Start the app
this.setVisible(true);
}
// This is an inner class that is used to detect a button press
class StartButtonListener implements ActionListener { // this is the
// required class
// definition
public void actionPerformed(ActionEvent event) {
System.out.println("Starting new Game");
thisFrame.dispose();
new GameFrame();
}
}
class InstructionButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Showing instructions");
thisFrame.dispose();
new Instructions();
}
}
}