forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcounter.java
92 lines (83 loc) · 2.85 KB
/
wordcounter.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
import java.awt.event.*;
import javax.swing.*;
public class WCC extends JFrame implements ActionListener{
JTextArea ta;
JButton b1,b2;
WCC(){
super("Word Character Counter - JavaTpoint");
ta=new JTextArea();
ta.setBounds(50,50,300,200);
b1=new JButton("Word");
b1.setBounds(50,300,100,30);
b2=new JButton("Character");
b2.setBounds(180,300,100,30);
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);add(b2);add(ta);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=ta.getText();
if(e.getSource()==b1){
String words[]=text.split("\\s");
JOptionPane.showMessageDialog(this,"Total words: "+words.length);
}
if(e.getSource()==b2){
JOptionPane.showMessageDialog(this,"Total Characters with space: "+text.length());
}
}
public static void main(String[] args) {
new WCC();
}
}
Word Count Example with Pad and Text Color
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CharCount extends JFrame implements ActionListener{
JLabel lb1,lb2;
JTextArea ta;
JButton b;
JButton pad,text;
CharCount(){
super("Char Word Count Tool - JTP");
lb1=new JLabel("Characters: ");
lb1.setBounds(50,50,100,20);
lb2=new JLabel("Words: ");
lb2.setBounds(50,80,100,20);
ta=new JTextArea();
ta.setBounds(50,110,300,200);
b=new JButton("click");
b.setBounds(50,320, 80,30);//x,y,w,h
b.addActionListener(this);
pad=new JButton("Pad Color");
pad.setBounds(140,320, 110,30);//x,y,w,h
pad.addActionListener(this);
text=new JButton("Text Color");
text.setBounds(260,320, 110,30);//x,y,w,h
text.addActionListener(this);
add(lb1);add(lb2);add(ta);add(b);add(pad);add(text);
setSize(400,400);
setLayout(null);//using no layout manager
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
String text=ta.getText();
lb1.setText("Characters: "+text.length());
String words[]=text.split("\\s");
lb2.setText("Words: "+words.length);
}else if(e.getSource()==pad){
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);
ta.setBackground(c);
}else if(e.getSource()==text){
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);
ta.setForeground(c);
}
}
public static void main(String[] args) {
new CharCount();
}}