-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingEligibility.java
More file actions
53 lines (45 loc) · 1.51 KB
/
Copy pathVotingEligibility.java
File metadata and controls
53 lines (45 loc) · 1.51 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VotingEligibility extends JFrame implements ActionListener {
JLabel label, resultLabel;
JTextField ageField;
JButton checkBtn;
JPanel panel = new JPanel(new GridLayout(3,2,5,5));
public VotingEligibility() {
setTitle("Voting Eligibility Checker");
setSize(300, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create components
label = new JLabel("Enter your age:");
ageField = new JTextField(10);
checkBtn = new JButton("Check Eligibility");
resultLabel = new JLabel("");
// Add action listener
checkBtn.addActionListener(this);
// Add components to the frame
panel.add(label);
panel.add(ageField);
panel.add(checkBtn);
panel.add(resultLabel);
add(panel);
}
public void actionPerformed(ActionEvent e) {
try {
int age = Integer.parseInt(ageField.getText());
if (age >= 18) {
resultLabel.setText("You are eligible to vote.");
} else {
resultLabel.setText("You are NOT eligible to vote.");
}
} catch (NumberFormatException ex) {
System.out.println("Please enter a valid number.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
VotingEligibility ve = new VotingEligibility();
ve.setVisible(true);
});
}
}