Skip to content

Commit 37a331a

Browse files
committed
Enhanced Word and Character Counter with additional features
1 parent b1491c9 commit 37a331a

File tree

1 file changed

+76
-92
lines changed

1 file changed

+76
-92
lines changed

wordcounter.java

Lines changed: 76 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,76 @@
1-
import java.awt.event.*;
2-
import javax.swing.*;
3-
public class WCC extends JFrame implements ActionListener{
4-
JTextArea ta;
5-
JButton b1,b2;
6-
WCC(){
7-
super("Word Character Counter - JavaTpoint");
8-
ta=new JTextArea();
9-
ta.setBounds(50,50,300,200);
10-
11-
b1=new JButton("Word");
12-
b1.setBounds(50,300,100,30);
13-
14-
b2=new JButton("Character");
15-
b2.setBounds(180,300,100,30);
16-
17-
b1.addActionListener(this);
18-
b2.addActionListener(this);
19-
add(b1);add(b2);add(ta);
20-
setSize(400,400);
21-
setLayout(null);
22-
setVisible(true);
23-
}
24-
public void actionPerformed(ActionEvent e){
25-
String text=ta.getText();
26-
if(e.getSource()==b1){
27-
String words[]=text.split("\\s");
28-
JOptionPane.showMessageDialog(this,"Total words: "+words.length);
29-
}
30-
if(e.getSource()==b2){
31-
JOptionPane.showMessageDialog(this,"Total Characters with space: "+text.length());
32-
}
33-
}
34-
public static void main(String[] args) {
35-
new WCC();
36-
}
37-
}
38-
Word Count Example with Pad and Text Color
39-
import java.awt.*;
40-
import javax.swing.*;
41-
import java.awt.event.*;
42-
public class CharCount extends JFrame implements ActionListener{
43-
JLabel lb1,lb2;
44-
JTextArea ta;
45-
JButton b;
46-
JButton pad,text;
47-
CharCount(){
48-
super("Char Word Count Tool - JTP");
49-
lb1=new JLabel("Characters: ");
50-
lb1.setBounds(50,50,100,20);
51-
lb2=new JLabel("Words: ");
52-
lb2.setBounds(50,80,100,20);
53-
54-
ta=new JTextArea();
55-
ta.setBounds(50,110,300,200);
56-
57-
b=new JButton("click");
58-
b.setBounds(50,320, 80,30);//x,y,w,h
59-
b.addActionListener(this);
60-
61-
pad=new JButton("Pad Color");
62-
pad.setBounds(140,320, 110,30);//x,y,w,h
63-
pad.addActionListener(this);
64-
65-
text=new JButton("Text Color");
66-
text.setBounds(260,320, 110,30);//x,y,w,h
67-
text.addActionListener(this);
68-
69-
add(lb1);add(lb2);add(ta);add(b);add(pad);add(text);
70-
71-
setSize(400,400);
72-
setLayout(null);//using no layout manager
73-
setVisible(true);
74-
setDefaultCloseOperation(EXIT_ON_CLOSE);
75-
}
76-
public void actionPerformed(ActionEvent e){
77-
if(e.getSource()==b){
78-
String text=ta.getText();
79-
lb1.setText("Characters: "+text.length());
80-
String words[]=text.split("\\s");
81-
lb2.setText("Words: "+words.length);
82-
}else if(e.getSource()==pad){
83-
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);
84-
ta.setBackground(c);
85-
}else if(e.getSource()==text){
86-
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);
87-
ta.setForeground(c);
88-
}
89-
}
90-
public static void main(String[] args) {
91-
new CharCount();
92-
}}
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
class WordCharCount extends JFrame implements ActionListener {
7+
JTextArea textArea;
8+
JButton wordButton, charButton, bgColorButton, textColorButton;
9+
10+
WordCharCount() {
11+
super("Word Character Counter - JavaTpoint");
12+
13+
textArea = new JTextArea();
14+
textArea.setBounds(50, 50, 300, 200);
15+
16+
wordButton = createButton("Word", 50, 300, 100, 30);
17+
charButton = createButton("Character", 180, 300, 100, 30);
18+
bgColorButton = createButton("Background Color", 50, 340, 150, 30);
19+
textColorButton = createButton("Text Color", 210, 340, 120, 30);
20+
21+
addComponents();
22+
addActionListeners();
23+
24+
setSize(400, 400);
25+
setLayout(null);
26+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27+
setVisible(true);
28+
}
29+
30+
private JButton createButton(String label, int x, int y, int width, int height) {
31+
JButton button = new JButton(label);
32+
button.setBounds(x, y, width, height);
33+
return button;
34+
}
35+
36+
private void addComponents() {
37+
add(textArea);
38+
add(wordButton);
39+
add(charButton);
40+
add(bgColorButton);
41+
add(textColorButton);
42+
}
43+
44+
private void addActionListeners() {
45+
wordButton.addActionListener(this);
46+
charButton.addActionListener(this);
47+
bgColorButton.addActionListener(this);
48+
textColorButton.addActionListener(this);
49+
}
50+
51+
public void actionPerformed(ActionEvent e) {
52+
String text = textArea.getText();
53+
if (e.getSource() == wordButton) {
54+
String[] words = text.split("\\s");
55+
showMessage("Total words: " + words.length);
56+
} else if (e.getSource() == charButton) {
57+
showMessage("Total Characters with space: " + text.length());
58+
} else if (e.getSource() == bgColorButton) {
59+
Color bgColor = JColorChooser.showDialog(this, "Choose Background Color", Color.WHITE);
60+
textArea.setBackground(bgColor);
61+
} else if (e.getSource() == textColorButton) {
62+
Color textColor = JColorChooser.showDialog(this, "Choose Text Color", Color.BLACK);
63+
textArea.setForeground(textColor);
64+
}
65+
}
66+
67+
private void showMessage(String message) {
68+
JOptionPane.showMessageDialog(this, message);
69+
}
70+
}
71+
72+
public class CharWordCountTool {
73+
public static void main(String[] args) {
74+
SwingUtilities.invokeLater(WordCharCount::new);
75+
}
76+
}

0 commit comments

Comments
 (0)