-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVowelCounterGUI.java
More file actions
115 lines (94 loc) · 3.49 KB
/
VowelCounterGUI.java
File metadata and controls
115 lines (94 loc) · 3.49 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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class VowelCounterGUI extends JFrame {
private JTextField inputField;
private JTextField resultField;
public VowelCounterGUI() {
// Set up the frame
setTitle("Vowel Counter");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create components
JLabel inputLabel = new JLabel("Enter a string:");
inputField = new JTextField(20);
JLabel resultLabel = new JLabel("Number of vowels:");
resultField = new JTextField(10);
resultField.setEditable(false);
JButton countButton = new JButton("Count Vowels");
JButton resetButton = new JButton("Reset");
JButton exitButton = new JButton("Exit");
// Add action listeners
countButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countVowels();
}
});
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resetFields();
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Create panels
JPanel mainPanel = new JPanel(new GridLayout(3, 1, 10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
inputPanel.add(inputLabel);
inputPanel.add(inputField);
JPanel resultPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
resultPanel.add(resultLabel);
resultPanel.add(resultField);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(countButton);
buttonPanel.add(resetButton);
buttonPanel.add(exitButton);
// Add panels to main panel
mainPanel.add(inputPanel);
mainPanel.add(resultPanel);
mainPanel.add(buttonPanel);
// Add main panel to frame
add(mainPanel);
// Show the frame
setVisible(true);
}
// Method to count vowels
private void countVowels() {
String input = inputField.getText();
int count = 0;
if (input != null && !input.isEmpty()) {
input = input.toLowerCase();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
}
resultField.setText(String.valueOf(count));
}
// Method to reset fields
private void resetFields() {
inputField.setText("");
resultField.setText("");
inputField.requestFocus();
}
public static void main(String[] args) {
// Run the GUI on the Event Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VowelCounterGUI();
}
});
}
}