Return to Snippet

Revision: 68242
at December 17, 2014 14:44 by acecengic


Updated Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class WordStats { 
 public static void main(String[] args) {
  JFrame window = new JFrame();
  window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  
 
  window.setSize(400, 500);

  final JTextArea TextArea = new JTextArea(); //area of the text input
  JScrollPane sp = new JScrollPane(TextArea); //scroll pane 
  sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //scrolling text box
  window.add(sp, BorderLayout.CENTER);

  JPanel p = new JPanel(); // panel "p" 
  p.setBorder(new TitledBorder("Word Statistics!!")); 
  JLabel l1 = new JLabel("Word Count:"); //basic labels for fields
  final JTextField tf1 = new JTextField(6); //length of the output field
  JLabel l2 = new JLabel("Average Word Length:");


  final JTextField tf2 = new JTextField(7);
  p.add(l1);
  p.add(tf1); 
  p.add(l2); 
  p.add(tf2);

  JButton b = new JButton("Stats!"); //button for showing the stats
  b.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = TextArea.getText();
    String[] ary = s.split("[\\s,.]");
    tf1.setText("" + ary.length);
    tf2.setText("" + (float)s.length()/ary.length);
   }
  });
  p.add(b);
  window.add(p, BorderLayout.SOUTH);

  window.setVisible(true);
 }
}

Revision: 68241
at December 17, 2014 14:42 by acecengic


Initial Code
/*
 * PP 5.16 Develop a simple tool for calculating basic statistics for a segment
of text. The application should have a single window with
a scrolling text box (a JTextArea) and a stats box. The stats box
should be a panel with a titled border, containing labeled fields
that display the number of words in the text box and the average
word length, as well as any other statistics that you would like
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class WordStats { 
 public static void main(String[] args) {
  JFrame window = new JFrame();
  window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  
 
  window.setSize(400, 500);

  final JTextArea TextArea = new JTextArea(); //area of the text input
  JScrollPane sp = new JScrollPane(TextArea); //scroll pane 
  sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //scrolling text box
  window.add(sp, BorderLayout.CENTER);

  JPanel p = new JPanel(); // panel "p" 
  p.setBorder(new TitledBorder("Word Statistics!!")); "
  JLabel l1 = new JLabel("Word Count:"); //basic labels for fields
  final JTextField tf1 = new JTextField(6); //length of the output field
  JLabel l2 = new JLabel("Average Word Length:");


  final JTextField tf2 = new JTextField(7);
  p.add(l1);
  p.add(tf1); 
  p.add(l2); 
  p.add(tf2);

  JButton b = new JButton("Stats!"); //button for showing the stats
  b.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String s = TextArea.getText();
    String[] ary = s.split("[\\s,.]");
    tf1.setText("" + ary.length);
    tf2.setText("" + (float)s.length()/ary.length);
   }
  });
  p.add(b);
  window.add(p, BorderLayout.SOUTH);

  window.setVisible(true);
 }
}

Initial URL

                                

Initial Description
Applet that does basic word processor functions

Initial Title
Java Word Processor

Initial Tags
java

Initial Language
Java