Java Word Processor


/ Published in: Java
Save to your folder(s)

Applet that does basic word processor functions


Copy this code and paste it in your HTML
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. public class WordStats {
  7. public static void main(String[] args) {
  8. JFrame window = new JFrame();
  9. window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  10.  
  11.  
  12. window.setSize(400, 500);
  13.  
  14. final JTextArea TextArea = new JTextArea(); //area of the text input
  15. JScrollPane sp = new JScrollPane(TextArea); //scroll pane
  16. sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //scrolling text box
  17. window.add(sp, BorderLayout.CENTER);
  18.  
  19. JPanel p = new JPanel(); // panel "p"
  20. p.setBorder(new TitledBorder("Word Statistics!!"));
  21. JLabel l1 = new JLabel("Word Count:"); //basic labels for fields
  22. final JTextField tf1 = new JTextField(6); //length of the output field
  23. JLabel l2 = new JLabel("Average Word Length:");
  24.  
  25.  
  26. final JTextField tf2 = new JTextField(7);
  27. p.add(l1);
  28. p.add(tf1);
  29. p.add(l2);
  30. p.add(tf2);
  31.  
  32. JButton b = new JButton("Stats!"); //button for showing the stats
  33. b.addActionListener(new ActionListener() {
  34. public void actionPerformed(ActionEvent e) {
  35. String s = TextArea.getText();
  36. String[] ary = s.split("[\\s,.]");
  37. tf1.setText("" + ary.length);
  38. tf2.setText("" + (float)s.length()/ary.length);
  39. }
  40. });
  41. p.add(b);
  42. window.add(p, BorderLayout.SOUTH);
  43.  
  44. window.setVisible(true);
  45. }
  46. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.