In: Computer Science
Graphical User Interfaces using java. Please provide proper commenting so that I understant what is going on in the program:
Develop a simple tool for analyzing a segment of text that determines the number of words in that segment and the average word length. The application should have a single window with a scrolling text box (JTextArea) and an area to display the statistics. The statistics area should be a panel with a titled border, containing labeled fields that display the number of words in the text and the average word length, as well as any other statistics you would like to add. The statistics area should also contain a button that, when pressed, computes the statistics for the current text content.
Hint: Use a Scanner object to parse the text content. The .next() operation will get the next word.
Design and implement an application that works as a stopwatch. Include a display that shows the time as it increments. The time value should be seconds only, to one decimal place. Include buttons that allow the user to start and stop the time, and to reset the display to zero. Arrange the components to present a nice user interface. The buttons should include a mnemonic shortcut. Only a single action listener should be used for the buttons.
Hi Dear
I'm writing the code which i have already tested and run on my pc.
Hope it helps and please like if you found my code helpful
Thanks
CODE:--
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; //importing different java classes
public class TextStatPanel extends JPanel implements ActionListener
{
private JTextArea textArea; //textArea for holding user text
private JButton updateButton; // to modify the value of text
private JLabel[] stats; //labels to show the modifications of text
public static final String[] statLabels =
{
"Total no. of words: ",
"avg. word length will be: "
};
public TextStatPanel()
{
textArea = new JTextArea(" ");
textArea.setWrapStyleWord(true);
textArea.setLineWrap (true);
updateButton = new JButton //make a button object to modify
("update text statistics");
updateButton.addActionListener (this); // action listener to handle the button event
JPanel statPane = new JPanel ();
statPane.setBorder (BorderFactory.createTitledBorder //setting border style
("Text Statistics"));
statPane.setOpaque(false); //making opaque false
int n = statLabels.length; //putting the statlabels to the panel
statPane.add(updateButton);
stats = new JLabel [n]; // alloting Jlabel to stats
for (int i=0; i<n; ++i)
{
JLabel label1 = new JLabel(statLabels[i]);
statPane.add(label1);
stats [i] = new JLabel ();
statPane.add(stats [i]);
}
JScrollPane scroll = new JScrollPane(textArea);
scroll.setPreferredSize(new Dimension (350, 400)); //fixing scroll bar area
scroll.setBorder(BorderFactory.createTitledBorder // setting border title
("Text Container (Enter the text below)"));
add (scroll); //adding scroll bar
add(statPane);
setBackground(new java.awt.Color(.33f, 0.45f, 0.65f)); //fixing background dimensions
setPreferredSize(new Dimension (400, 600));
updateStatistics(); //calling update statistics
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == updateButton)
{
updateStatistics();
}
}
private void updateStatistics()
{
String text = textArea.getText();
String[] words = text.split(" ");
float ave =
(text.length() -words.length+0.0f)/words.length;
stats[0].setText (String.valueOf(words.length));
stats[1].setText (String.valueOf(ave));
}//end of updateStatistics
}//end of class TextStatPanel
import javax.swing.JFrame; //TextStatDriver.java
public class TextStatDriver
{
public static void main(String[] args) //Creates and displays the main program
{
JFrame frame = new JFrame("Tool for calculating " +
"basic ststistic segment of text:");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( new TextStatPanel());
frame.pack();
frame.setVisible(true);
}
}