26
JTextArea formatting text areas

JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

  • View
    236

  • Download
    0

Embed Size (px)

Citation preview

Page 1: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

JTextArea

formatting text areas

Page 2: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

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.

Page 3: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Example of JTextArea inputJTextArea 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);

Page 4: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Display of showInputDialog

Input

Calculator for x and yEnter your selection1. Addition2. Subtraction3. Multiplication4. Quit

OK Cancel

??

Page 5: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

JTextArea sizeThe 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.

Page 6: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

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.

Page 7: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

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);

Page 8: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Input data

Page 9: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Array: numbers[0] to numbers[8]

Page 10: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Display the data

Page 11: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

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

}

Page 12: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Bubble Sort

Page 13: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Bubble – use the previous template

Page 14: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Sample

Page 15: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Question 2 – Rational

Page 16: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Question 2 – Rational

Page 17: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Output

Page 18: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Output

Page 19: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Rational – Add

Page 20: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Rational – Compare

Page 21: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Rational – Common divisor

Page 22: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Test Driver - input

Page 23: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Test driver – case 1

Page 24: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Test driver – case 2

Page 25: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

Test Driver – case 3

Page 26: JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape

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: { Rational r1; int a = Integer.parseInt(JOptionPane.showInputDialog( "Enter numerator of Rational" )); int b = Integer.parseInt(JOptionPane.showInputDialog( "Enter denominator of Rational" )); ......................

} break; case 2: { 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( "Enter denominator of Second Rational" ));

............................ } break; // done processing case

case 3: { 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( "Enter denominator of Second Rational" ));

..................

} break; // done processing case default: output = "Invalid value entered"; } // end switch JOptionPane.showMessageDialog( null, output, "Results", JOptionPane.INFORMATION_MESSAGE ); } }