270 likes | 476 Views
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 <br> and t. Can be used in showInputDialog and showMessageDialog methods taking the place of the String output parameter.
E N D
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 JTextAreaoutputArea = newJTextArea(); 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 • Addition • Subtraction • Multiplication • 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);
Array: numbers[0] to numbers[8] numbers[0] numbers[1] numbers[2] ...... numbers[7] numbers[8] 9
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 • }
Output 1 reduce
Output 2 equal such as 1/2 not equal to 3/4 3 add 1/2 + 3/4 = 5/4
Test driver – case 1 case 1
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 ); • } • }