I need some help creating a program on java, I have posted my instructors questi
ID: 3759180 • Letter: I
Question
I need some help creating a program on java, I have posted my instructors question.
I need to use JFrame, with the extends JPanel
Write a program in Java that computes the number of words in a text entered in a text field. Use the following graphical objects:
a text field for the input text (one line of text)
labels to output the word count
a button that, when pressed, computes the word count
a button that, when pressed, clears the text field
a panel, that includes all graphical objects listed above
Make your application run in a window (JFrame) and use a proper layout, backgrounds and colors to make all objects clearly visible.
Extra credit (maximum 2 points) will be given for additional text statistics (for example, maximum, minimum and average word size, number of numeric tokens, word frequencies etc.).
Documentation: Run your program and make sure it compiles without errors and works as described above. Then add your name in the beginning of the code as a comment. Add also comments to explain the classes and methods used. Note that comments and the way you format your program will be graded too.
Submission: Submit the following files as attachments:
The source code (.java) of all classes used.
A screen copy (an MS Word file with the image included) of the BlueJ Programs window showing all classes used with the relations between them.
A screen copy showing an example run of your program.
Explanation / Answer
import java.awt.*; import java.awt.event.*; public class AWTCounterAnonymousInnerClass extends Frame { // This class is NOT the listener, hence, it does not implement ActionListener private TextField tfCount; private int count = 0; // Constructor to setup the UI public AWTCounterAnonymousInnerClass () { setLayout(new FlowLayout()); // "this" Frame sets to FlowLayout add(new Label("Counter")); // anonymous Label tfCount = new TextField(count + "", 10); tfCount.setEditable(false); // read-only add(tfCount); // this Frame adds tfCount Button btnCount = new Button("Count"); add(btnCount); // this Frame adds btnCount // Construct an anonymous instance of an anonymous class as // listener to the source btnCount btnCount.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ++count; tfCount.setText(count + ""); } }); setSize(250, 100); setTitle("AWT Counter"); setVisible(true); // show it } public static void main(String[] args) { new AWTCounterAnonymousInnerClass(); } }