Transcript Lab9

JTextArea
formatting text areas
Using JTextArea



JTextArea class is found in the
javax.swing package.
Creates an text area that can process
the escape formatting of \n and \t.
Can be used in showInputDialog and
showMessageDialog methods taking the
place of the String output parameter.
Example of JTextArea input
JTextArea outputArea = new JTextArea();
int choice;
do {
String instructions ="Calculator for x and y "
+ "\nEnter your selection \n 1. Addition "
+ "\n 2. Subtraction \n 3. Multiplication "
+ "\n 4. Quit";
outputArea.setText(instructions);
String choiceStr =
OptionPane.showInputDialog(outputArea);
Display of showInputDialog
Input
?
Calculator for x and y
Enter your selection
1. Addition
2. Subtraction
3. Multiplication
4. Quit
OK
Cancel
JTextArea size
The statement:
JTextArea outputArea = new JTextArea();
Defaults the side of the text area to fit the string
to be displayed.
Specify the size by using width and height
JTextArea outputArea = new JTextArea(40, 100);
Sets aside a 40 pixel width and 100 pixel height
area of text.
JTextArea set and append
Once the JTextArea object has been
created with the new command, your
code can initialize the text value in the
JTextArea with the set method.
You can add to the text by using the
append method.
JTextArea set and append
JTextArea outputArea = new JTextArea();
outputArea.setText (“Title: 1 to 5”);
for (int x = 1; x <= 5; ++x)
outputArea.append(“\n” + x );
outputArea.append(“\nTHE END”);
JOptionPane.showMessageDialog(null,
outputArea);
Input data
Array: numbers[0] to numbers[8]
Display the data
Sample of program



import java.awt.Container;
import javax.swing.*;
public class lab91 extends JApplet {
double[] numbers;
String output="";


public void init()
{
numbers = new double[9];
for (int i = 0; i < 9; i++) {
numbers[i] = Double.parseDouble(JOptionPane.showInputDialog(
"Enter a floating-point value" ));
}







for (int i = 0; i < 9; i++) {
output += "number" + (i + 1) +": "+numbers[i]+"\n";
}



JTextArea outputArea = new JTextArea(150, 300);
outputArea.setText( output);
Container container = getContentPane();
container.add( outputArea );




} // end method init




}
Bubble Sort
Bubble – use the previous template
Sample
Question 2 – Rational
Question 2 – Rational
Output
Output
Rational – Add
Rational – Compare
Rational – Common divisor
Test Driver - input
Test driver – case 1
Test driver – case 2
Test Driver – case 3
Software

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class GUIRationalDriver extends JApplet {






public void init() {
String input,output;
int choice;


input = JOptionPane.showInputDialog(
"Enter 1 to test reduce() method\n" +
"Enter 2 to test equal() method\n" +
"Enter 3 to test add() method\n");

choice = Integer.parseInt( input );

switch ( choice ) {

case 1: {




















break;
case 2: {
Rational r1;
int a = Integer.parseInt(JOptionPane.showInputDialog(
"Enter numerator of Rational" ));
int b = Integer.parseInt(JOptionPane.showInputDialog(
"Enter denominator of Rational" ));
......................
}
Rational r1,r2;
int a = Integer.parseInt(JOptionPane.showInputDialog(
"Enter numerator of First Rational" ));
int b = Integer.parseInt(JOptionPane.showInputDialog(
"Enter denominator of First Rational" ));
int c = Integer.parseInt(JOptionPane.showInputDialog(
"Enter numerator of Second Rational" ));
int d = Integer.parseInt(JOptionPane.showInputDialog(