Java know how much (86) input and output of text box and text area

Source: Internet
Author: User

In the GUI, common text boxes and text areas implement data input and output. If text area input is used, a data entry completion button is usually set up separately. When the data entry is finished, click this button. The event handler uses the GetText () method to read string information from the text area. For cases where a text box is used as input, the last entered carriage return can fire an input completion event, usually without a separate button. The event handler can use the word parser to parse out a number, and then use the string to convert the numerical method to obtain the input value. For output, the program first converts the numeric value to a string and then outputs the data to a text box or text area through the SetText () method.

Example 11-9 Small application sets a text area, a text box, and two buttons. The user enters an integer sequence in the text area, clicks the Sum button, the program sums the integer sequence in the text area, and outputs the sum in the text box. Click the second button to clear the text area and the contents of the text box.

1 Importjava.util.*;Importjava.applet.*;Importjava.awt.*;2 Importjavax.swing.*;Importjava.awt.event.*;3  Public classJ509extendsAppletImplementsactionlistener{4 JTextArea texta; JTextField TEXTF; JButton b1,b2;5      Public voidinit () {6SetSize (250,150);7Texta=NewJTextArea ("", 5,10);8 Texta.setbackground (Color.cyan);9textf=NewJTextField ("", 10);Ten Textf.setbackground (color.pink); Oneb1=NewJButton ("sum"); B2=NewJButton ("Re-start"); ATextf.seteditable (false); -B1.addactionlistener ( This); B2.addactionlistener ( This); - Add (texta); add (TEXTF); Add ( B1); Add (B2) ; the     } -      Public voidactionperformed (ActionEvent e) { -         if(E.getsource () = =B1) { -String s=Texta.gettext (); +StringTokenizer tokens=NewStringTokenizer (s); -             //Use the default delimiter collection: spaces, line breaks, tab matches carriage return as separators +             intN=tokens.counttokens (), sum=0, I; A              for(i=0;i<=n-1;i++){ atString Temp=tokens.nexttoken ();//remove a data from the text area -sum+=Integer.parseint (temp); -             }  -Textf.settext ("" +sum); -         } -         Else if(E.getsource () = =B2) { inTexta.settext (NULL); -Textf.settext (NULL); to         } +     } -}

The "Example 11-10" applet calculates all the number of factor multiples from the starting integer to the terminating integer. The applet container uses a GridLayout layout to divide the interface into 3 rows, the first row is the label, the second row and the third row are two panel. Design two panel container class Panel1,panel2, and separately with GridLayout layout division. Panel1 is 1 rows, 6 columns, and PANEL2 is 1 rows and 4 columns. The labels and the components generated by the container class Panel1,panel2 are then added to the appropriate location in the window.

1 Importjava.applet.*;Importjavax.swing.*;2 Importjava.awt.*;Importjava.awt.event.*;3 classPanel1extendsjpanel{4 JTextField text1,text2,text3;5Panel1 () {//construction method. When you create a panel object, the panel is initialized to have three labels6         //three text boxes, layout GridLayout (1,6)7text1=NewJTextField (text2=);NewJTextField (10);8text3=NewJTextField (setlayout);NewGridLayout (1,6));9AddNewJLabel ("Start number", Jlabel.right)); Add (Text1);TenAddNewJLabel ("Number of stops", Jlabel.right)); Add (text2); OneAddNewJLabel ("factor", Jlabel.right)); Add (TEXT3); A     } - } - classPanel2extendsjpanel{//Extended Panel class the JTextArea text; JButton Button; -Panel2 () {//construction method. When a panel object is created, the panel is initialized to have a label -         //a text box with a layout of GridLayout (1,4) -text=NewJTextArea (4,10); Text.setlinewrap (true); +JScrollPane jsp=NewJScrollPane (text); -button=NewJButton ("Start calculation"); +SetLayout (NewGridLayout (1,4)); AAddNewJLabel ("Calculated result:", Jlabel.right)); at Add (JSP); -AddNewLabel ()); Add (Button); -     } - } -  Public classJ510extendsAppletImplementsactionlistener{ - Panel1 Panel1; Panel2 Panel2; in      Public voidinit () { -SetLayout (NewGridLayout (3,1)); toSetSize (400,200);p anel1=NewPanel1 ();p anel2=NewPanel2 (); +AddNewJLabel ("Count from start to end number is factor multiplier", Jlabel.center)); - Add (Panel1); add (Panel2); the(Panel2. Button). addActionListener ( This); *     } $      Public voidactionperformed (ActionEvent e) {Panax Notoginseng         if(E.getsource () = =(Panel2. Button)) { -             LongN1,n2,f,count=0; then1=Long.parselong (Panel1.text1.getText ()); +N2=Long.parselong (Panel1.text2.getText ()); Af=Long.parselong (Panel1.text3.getText ()); the              for(Longi=n1;i<=n2;i++){ +                 if(i%f==0) -Panel2.text.append (string.valueof (i) + ""); $            } $         } -     } -}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text area

Java know how much (86) input and output of text box and text area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.