-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelcomePage.java
More file actions
82 lines (70 loc) · 2.45 KB
/
WelcomePage.java
File metadata and controls
82 lines (70 loc) · 2.45 KB
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class WelcomePage extends JFrame implements ActionListener {
//Initialize the components
JLabel mainLabel;
JButton playButton;
JButton instructionsButton;
//Initialize the constructor
WelcomePage(){
//Initialize first panel and add main label
mainLabel = newLabel("Welcome to the Number Guessing Game!", 25);
JPanel panel1 = newPanel();
panel1.add(mainLabel);
//Initialize the second panel and add buttons
playButton = newButton("Play");
instructionsButton = newButton("Instructions");
JPanel panel2 = newPanel();
panel2.add(playButton);
panel2.add(instructionsButton);
//Add the panels to the frame and customize it
add(panel1);
add(panel2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
setSize(500, 500);
setVisible(true);
setLocationRelativeTo(null);
}
//Method to make and customize a new button
private JButton newButton(String text){
JButton button = new JButton(text);
button.setBackground(Color.BLUE);
button.setForeground(Color.WHITE);
button.setFont(new Font(null, Font.BOLD, 20));
button.setFocusable(false);
button.addActionListener(this);
return button;
}
//Method to make and customize a new panel
private JPanel newPanel(){
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setOpaque(true);
panel.setLayout(new FlowLayout());
return panel;
}
//Method to make and customize a new label
private JLabel newLabel(String text, int size){
JLabel label = new JLabel();
label.setForeground(Color.WHITE);
label.setFont(new Font(null, Font.PLAIN, size));
label.setText(text);
return label;
}
//Action Performed Method
@Override
public void actionPerformed(ActionEvent e) {
//If playButton is pressed
if (e.getSource() == playButton){
new NumberGuesser();
dispose();
} else if (e.getSource() == instructionsButton) {
//If instructionsButton is pressed
new Instructions();
dispose();
}
}
}