-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWelcomeScreen.java
164 lines (135 loc) · 7.47 KB
/
WelcomeScreen.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class WelcomeScreen extends JFrame {
private JTextField txtUsername;
private JPasswordField txtPassword;
private HashMap<String, String> credentials;
public WelcomeScreen() {
setTitle("Welcome to EPICUREAN EATS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
credentials = new HashMap<>();
credentials.put("admin", "password");
JLabel lblWelcome = new JLabel("Welcome to Epicurean Eats!");
lblWelcome.setFont(new Font("Georgia", Font.BOLD, 28));
lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
lblWelcome.setForeground(Color.WHITE);
JLabel lblUsername = new JLabel("Username:");
txtUsername = new JTextField(20);
lblUsername.setFont(new Font("Georgia", Font.BOLD, 12));
lblUsername.setForeground(Color.WHITE);
JLabel lblPassword = new JLabel("Password:");
txtPassword = new JPasswordField(20);
lblPassword.setFont(new Font("Georgia", Font.BOLD, 12));
lblPassword.setForeground(Color.WHITE);
/**
* Represents the Login button on the Welcome Screen.
* When clicked, it validates the entered username and password
* and opens the Main Menu Screen if the credentials are correct.
* Otherwise, it displays an error message.
*/
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Check the username and password
if (validateCredentials(txtUsername.getText(), new String(txtPassword.getPassword()))) {
// Open the Main Menu screen if credentials are correct
//openMainMenuScreen();
openMainMenu();
} else {
// Display error message if credentials are incorrect
JOptionPane.showMessageDialog(WelcomeScreen.this, "Invalid username or password", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
// Add hover effect
btnLogin.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
btnLogin.setBackground(new Color(255, 204, 0));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
btnLogin.setBackground(UIManager.getColor("control")); // Reset to default color
}
});
JPanel panel = new JPanel();
panel.setBackground(new Color( 222, 184, 135));
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblWelcome, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(lblUsername, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblPassword, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(txtUsername)
.addComponent(txtPassword, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
.addContainerGap(50, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(160, Short.MAX_VALUE)
.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
.addContainerGap(160, Short.MAX_VALUE))
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblWelcome)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblUsername)
.addComponent(txtUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblPassword)
.addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnLogin)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addContainerGap(30, Short.MAX_VALUE))
);
add(panel);
pack();
setLocationRelativeTo(null);
}
private boolean validateCredentials(String username, String password) {
// Check if the entered username exists in the credentials map,
// and if its corresponding password matches the entered password
return credentials.containsKey(username) && credentials.get(username).equals(password);
}
private void openMainMenu() {
MainMenu mainMenu = new MainMenu();
mainMenu.setVisible(true);
dispose();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
WelcomeScreen welcomeScreen = new WelcomeScreen();
welcomeScreen.setVisible(true);
});
}
}