Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape."— Presentation transcript:

1 JTextArea formatting text areas

2 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.

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

4 Display of showInputDialog Input Calculator for x and y Enter your selection 1.Addition 2.Subtraction 3.Multiplication 4.Quit OKCancel ?

5 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.

6 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.

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

8 Input data

9 Array: numbers[0] to numbers[8]

10 Display the data

11 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 }

12 Bubble Sort

13 Bubble – use the previous template

14 Sample

15 Question 2 – Rational

16

17 Output

18

19 Rational – Add

20 Rational – Compare

21 Rational – Common divisor

22 Test Driver - input

23 Test driver – case 1

24 Test driver – case 2

25 Test Driver – case 3

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


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

Similar presentations


Ads by Google