I want to make the JText area scrollable.. but my result doesn't work in that way. help me plz.
package m5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TextAnalyzer extends JFrame implements
ActionListener
{
//Initializes component variables for the
frame
//For text typing area
JLabel type = new JLabel("Type sentences in the box
below.", JLabel.CENTER);
JTextArea txt = new JTextArea(10, 10);
JPanel txt1 = new JPanel();
//For Statistic data area with a panel
JLabel nwords = new JLabel("Number of words: ");
JPanel nwords2 = new JPanel();
JLabel lwords = new JLabel("Average words length:
");
JPanel lwords2 = new JPanel();
JLabel nchars = new JLabel("Number of characters:
");
JPanel nchars2 = new JPanel();
JButton button = new JButton("Show Statistics!");
JPanel stat = new JPanel();
public TextAnalyzer()
{
setTitle("COMP Assigmnet
M5-1");
type.setFont(new Font("Helvetica",
Font.BOLD, 15));
//Sets texts in the JTextArea would
be wrapped
txt.setLineWrap(true);
txt.setWrapStyleWord(true);
//Sets BorderLayout as a layout for
the text typing area
txt1.setLayout(new
BorderLayout());
txt1.setAlignmentX(Component.CENTER_ALIGNMENT);
txt1.add(type,
BorderLayout.NORTH);
txt1.add(txt, BorderLayout.CENTER);
//Sets BoxLayout as a layout for
the statistics area
//Added a titled border using
BorderFactory
//Added small area between labels
using Box.createRigidArea methods
Box statBox = new
Box(BoxLayout.Y_AXIS);
statBox.setAlignmentX(Component.CENTER_ALIGNMENT);
setLayout(new BoxLayout(statBox,
BoxLayout.Y_AXIS));
statBox.setBorder(BorderFactory.createTitledBorder("Statistics"));
statBox.add(Box.createRigidArea(new
Dimension(8,5)));
statBox.add(nwords);
statBox.add(Box.createRigidArea(new
Dimension(8,5)));
statBox.add(nchars);
statBox.add(Box.createRigidArea(new
Dimension(8,5)));
statBox.add(lwords);
statBox.add(Box.createRigidArea(new
Dimension(8,20)));
statBox.add(button);
statBox.add(Box.createRigidArea(new
Dimension(8,20)));
//Sets BorderLayout as a layout for
the whole window
setLayout(new
BorderLayout());
add(txt1, BorderLayout.CENTER);
add(statBox, BorderLayout.SOUTH);
//Sets button as a action
listener
button.addActionListener(this);
//Sets for the window to be closed
by clicking 'x' button on the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Defines actionPerfomed methods with a possible
exception
//Defines actions that would happen when the listener
events occur
public void actionPerformed(ActionEvent evt) throws
ArithmeticException
{
//Sets input segment as a input
data of text area
//Makes a string list 'input' for
the scanner to save segments in that obtained by next()
method
String segment =
txt.getText();
Scanner scan = new
Scanner(segment);
ArrayList <String> input =
new ArrayList<String>();
int wordLength = 0;
//Receive number of words by
getting the size of the arraylist
while (scan.hasNext())
input.add(scan.next());
nwords.setText("Number of words: "
+ input.size());
//Iterates items inside the
arraylist and accumulates the number of whole characters by
string.length() method to get number of characters
for (String items: input)
wordLength +=
items.length();
nchars.setText("Number of
characters: " + wordLength);
//Gets average words length by
dividing number of characters by number of words
//Uses try and catch block to catch
a exception that could be thrown when the nothing was written in
the text area
double a = 0;
try
{ a = wordLength/input.size();
}
catch (ArithmeticException
problem)
{ a = 0; }
finally
{ lwords.setText("Average words
length: " + a); }
}
//Initiates an object of TextAnalyzer as a JFrame and
sets its size
public static void main(String[] args)
{
TextAnalyzer app = new
TextAnalyzer();
app.pack();
app.setSize(500, 400);
app.setVisible(true);
}
}
/*****************************TextAnalyzer.java***********************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TextAnalyzer extends JFrame implements ActionListener {
// Initializes component variables for the frame
// For text typing area
JLabel type = new JLabel("Type sentences in the box
below.", JLabel.CENTER);
JTextArea txt = new JTextArea(10, 10);
JScrollPane scroll = new JScrollPane(txt);//add
scrollPane
JPanel txt1 = new JPanel();
// For Statistic data area with a panel
JLabel nwords = new JLabel("Number of words: ");
JPanel nwords2 = new JPanel();
JLabel lwords = new JLabel("Average words length:
");
JPanel lwords2 = new JPanel();
JLabel nchars = new JLabel("Number of characters:
");
JPanel nchars2 = new JPanel();
JButton button = new JButton("Show Statistics!");
JPanel stat = new JPanel();
public TextAnalyzer() {
setTitle("COMP Assigmnet
M5-1");
type.setFont(new Font("Helvetica",
Font.BOLD, 15));
// Sets texts in the JTextArea
would be wrapped
txt.setLineWrap(true);
txt.setWrapStyleWord(true);
// Sets BorderLayout as a layout
for the text typing area
txt1.setLayout(new
BorderLayout());
txt1.setAlignmentX(Component.CENTER_ALIGNMENT);
txt1.add(type,
BorderLayout.NORTH);
txt1.add(scroll,
BorderLayout.CENTER);
// Sets BoxLayout as a layout
for the statistics area
// Added a titled border using
BorderFactory
// Added small area between labels
using Box.createRigidArea methods
Box statBox = new
Box(BoxLayout.Y_AXIS);
statBox.setAlignmentX(Component.CENTER_ALIGNMENT);
setLayout(new BoxLayout(statBox,
BoxLayout.Y_AXIS));
statBox.setBorder(BorderFactory.createTitledBorder("Statistics"));
statBox.add(Box.createRigidArea(new
Dimension(8, 5)));
statBox.add(nwords);
statBox.add(Box.createRigidArea(new
Dimension(8, 5)));
statBox.add(nchars);
statBox.add(Box.createRigidArea(new
Dimension(8, 5)));
statBox.add(lwords);
statBox.add(Box.createRigidArea(new
Dimension(8, 20)));
statBox.add(button);
statBox.add(Box.createRigidArea(new
Dimension(8, 20)));
// Sets BorderLayout as a layout
for the whole window
setLayout(new
BorderLayout());
add(txt1,
BorderLayout.CENTER);
add(statBox,
BorderLayout.SOUTH);
// Sets button as a action
listener
button.addActionListener(this);
// Sets for the window to be
closed by clicking 'x' button on the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Defines actionPerfomed methods with a possible
exception
// Defines actions that would happen when the listener
events occur
public void actionPerformed(ActionEvent evt) throws
ArithmeticException {
// Sets input segment as a input
data of text area
// Makes a string list 'input' for
the scanner to save segments in that obtained
// by next() method
String segment =
txt.getText();
Scanner scan = new
Scanner(segment);
ArrayList<String> input = new
ArrayList<String>();
int wordLength = 0;
// Receive number of words by
getting the size of the arraylist
while (scan.hasNext())
input.add(scan.next());
nwords.setText("Number of words: "
+ input.size());
// Iterates items inside the
arraylist and accumulates the number of whole
// characters by string.length()
method to get number of characters
for (String items : input)
wordLength +=
items.length();
nchars.setText("Number of
characters: " + wordLength);
// Gets average words length by
dividing number of characters by number of words
// Uses try and catch block to
catch a exception that could be thrown when the
// nothing was written in the text
area
double a = 0;
try {
a = wordLength /
input.size();
} catch (ArithmeticException
problem) {
a = 0;
} finally {
lwords.setText("Average words length: " + a);
}
}
// Initiates an object of TextAnalyzer as a JFrame
and sets its size
public static void main(String[] args) {
TextAnalyzer app = new
TextAnalyzer();
app.pack();
app.setSize(500, 400);
app.setVisible(true);
}
}
/********************output*********************/
Please let me know if you have any doubt or modify the answer, Thanks :)
I want to make the JText area scrollable.. but my result doesn't work in that way....
With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...
Please fix and test my code so that all the buttons and function are working in the Sudoku. CODE: SudokuLayout.java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SudokuLayout extends JFrame{ JTextArea txtArea;//for output JPanel gridPanel,butPanel;//panels for sudoku bord and buttons JButton hint,reset,solve,newPuzzel;//declare buttons JComboBox difficultyBox; SudokuLayout() { setName("Sudoku Bord");//set name of frame // setTitle("Sudoku Bord");//set...
Modify the code to turn the text area background to the color YELLOW if you click an "x" (lower or uppercase)? package com.java24hours; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class KeyViewer extends JFrame implements KeyListener { JTextField keyText = new JTextField(80); JLabel keyLabel = new JLabel("Press any key in the text field."); public KeyViewer() { super("KeyViewer"); set LookAndFeel(); setSize(350, 100); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; keyText.addKeyListener(this); BorderLayout bord = new BorderLayout(); set Layout (bord); add (keyLabel, BorderLayout. NORTH); add (keyText, BorderLayout....
I have this problem for a final thats in my book thats i'm stuck on . Any help would be really appreciated. Design and code a Swing GUI to translate text that is input in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: ⦁ For words that begin with consonants, move the leading consonant to the end of the word and add “ay.” For example, “ball” becomes...
i want answers please ??? Answers to Self-Review Exercises 625 14.3 d) In a BorderLayout, two buttons added to the NORTH region will be placed side by side. e) A maximum of five components can be added to a BorderLayout ) Inner classes are not allowed to access the members of the enclosing class. B) A TextArea's text is always read-only. h) Class TextArea is a direct subclass of class Component. Find the error(s) in each of the following statements,...
Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off. Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...
JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...
Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file. Program...
Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...
I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener? There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...