|
| 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 | +}} |
0 commit comments