1 / 26

JTextArea

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.

hua
Download Presentation

JTextArea

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  4. Display of showInputDialog Input ? • Calculator for x and y • Enter your selection • Addition • Subtraction • Multiplication • Quit OK Cancel

  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] numbers[0] numbers[1] numbers[2] ...... numbers[7] numbers[8] 9

  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. Question 2 – Rational

  17. Output 1 reduce

  18. Output 2 equal such as 1/2 not equal to 3/4 3 add 1/2 + 3/4 = 5/4

  19. Rational – Add

  20. Rational – Compare

  21. Rational – Common divisor

  22. Test Driver - input

  23. Test driver – case 1 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 ); • } • }

More Related