Skip to content

Commit

Permalink
Enhanced Word and Character Counter with additional features
Browse files Browse the repository at this point in the history
  • Loading branch information
KhwajaYousuf committed Feb 1, 2024
1 parent b1491c9 commit 37a331a
Showing 1 changed file with 76 additions and 92 deletions.
168 changes: 76 additions & 92 deletions wordcounter.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,76 @@
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();
}}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class WordCharCount extends JFrame implements ActionListener {
JTextArea textArea;
JButton wordButton, charButton, bgColorButton, textColorButton;

WordCharCount() {
super("Word Character Counter - JavaTpoint");

textArea = new JTextArea();
textArea.setBounds(50, 50, 300, 200);

wordButton = createButton("Word", 50, 300, 100, 30);
charButton = createButton("Character", 180, 300, 100, 30);
bgColorButton = createButton("Background Color", 50, 340, 150, 30);
textColorButton = createButton("Text Color", 210, 340, 120, 30);

addComponents();
addActionListeners();

setSize(400, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

private JButton createButton(String label, int x, int y, int width, int height) {
JButton button = new JButton(label);
button.setBounds(x, y, width, height);
return button;
}

private void addComponents() {
add(textArea);
add(wordButton);
add(charButton);
add(bgColorButton);
add(textColorButton);
}

private void addActionListeners() {
wordButton.addActionListener(this);
charButton.addActionListener(this);
bgColorButton.addActionListener(this);
textColorButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
String text = textArea.getText();
if (e.getSource() == wordButton) {
String[] words = text.split("\\s");
showMessage("Total words: " + words.length);
} else if (e.getSource() == charButton) {
showMessage("Total Characters with space: " + text.length());
} else if (e.getSource() == bgColorButton) {
Color bgColor = JColorChooser.showDialog(this, "Choose Background Color", Color.WHITE);
textArea.setBackground(bgColor);
} else if (e.getSource() == textColorButton) {
Color textColor = JColorChooser.showDialog(this, "Choose Text Color", Color.BLACK);
textArea.setForeground(textColor);
}
}

private void showMessage(String message) {
JOptionPane.showMessageDialog(this, message);
}
}

public class CharWordCountTool {
public static void main(String[] args) {
SwingUtilities.invokeLater(WordCharCount::new);
}
}

0 comments on commit 37a331a

Please sign in to comment.