In GUI, the input and output of data in the text box and text area are commonly used. If the text area is input, another data input complete button is usually set. When the data input ends, click this button. Event processing program uses the gettext () method to read string information from the text area. For the use of the text box as the input, the final input return to the car can stimulate the input completion event, and there is usually no need to set the button. Event processing program can use the word analyzer to analyze the number, and then use the string to convert the numerical method to obtain the input value. For the output, the program first converts the value into a string, and then outputs the data to the text box or text area by the settext () method.
[Example 11-9] Small applications set a text area, a text box and two buttons. The user enters an integer sequence in the text area, click on the button, and the program sequels the integer sequence in the text zone, and is output and output in the text box. Click the second button to clear the content of the text area and the text box.
Import java.util.*; Import java.applet.*; Import java.awt.*; Import Javax.swing.*; Import java.awt.event.*; Let Implements actionListener {jtextarea texta; jtextField textf ; JBUTTON B1, B2; Public Void Init () {setsize (250,150); TextA = New Jtextarea ("", 5,10); TextA.SetbackGround (color.cyan); TextF = NEW JTE XTField ("", 10); TextF.SetbackGround (color.pink); B1 = New Jbutton ("Seeking"); B2 = New Jbutton ("Re -Start"); TextF.SetEditable (FALSE); B1.AdDactionListener (this); B. 2.AdDactionListener (this (this ); add (texta); add (textf); add (b1); add (b2);} Public void ActionPerFormed (ACTIONEVENT E) {if (e.getsource () == B1) {string s = texta.get Text ( ).; 0; I <= n-1; I ++) {string test = tokens.nextToken (); // Remove a data SUM+= Integer.parseint (Temp) from the text area;} TextF.Settext (""+Sum) } Else if (e.getsource () == B2) {texta.setText (null); textf.setText (null);}}}
[Example 11-10] Small application calculations are all the number of factor multiple from the starting integer to the termination integer. The small program container uses GridLayout layout to divide the interface into three ranks. The first line is the label, and the second and third rows are two Panel. Design two Panel container class Panel1, Panel2, and divide it with GridLayout. Panel1 is 1 row 6 column, and Panel2 is 1 line 4 column. The components generated by the label and the container Panel1 and Panel2 are then added to the corresponding position of the window.
Import java.applet.*; Import javax.swing.*; Import Java.awt.*; Import Java.awt.event.*; Class Panel1 Extends Jpanel {JTEXTFIELD Text1, TEXT2, T2, T2, T EXT3; Panel1 () {// Construction method Essence When the Panel object is created, the Panel is initialized to have three labels // three text boxes, and the layout is GridLayout (1,6) Text1 = New JtextField (10); Text2 = New JtextField (10); Text3 = New JTextField (10 );; New GridLayout (1,6)); Add (New Jlabel ("starting number", JLABEL.RIGHT); ADD (Text1); add ("end jlabel (" Termid number ", jlabel.right))) ; add (text2); add (new jlabel ("factor", jlabel.richt); add (text3);} Class Panel2 Extends jpanel {// expansion. () {// Construction method. When creating a Panel object, Panel was initialized to have a label // a text box, layout to GridLayout (1,4) Text = New Jtextarea (4,10); Text.SetLinewrap (TRUE); jscrollpane jsp = new jscroll Pane (Pane ( text); Button = New Jbutton ("Starting Calculation"); SetLayout (New GridLayout (1,4)); add jlabel ("Calculation Result:", JLABEL.RIGHT); Add (JSP); Label ()); add (Button);}} Public Class J510 Extends Applets ActionListener {Panel1 Panel1; Panel2 Panel2; Public Void Init () {Setlay (NEW GR idlayout (3,1)); setsize (400,200); Panel1 = New Panel1 (); Panel2 = New Panel2 (); Add (New Jlabel ("Calculation from the number of starting to the end to the end is the number of factor multiple", jlabel.center); add (Panel1); add (Panel2); (Panel2); Panel2.Button) .adDactionListener (this);} Public Void ActionPerFormed (ActionEvent E) {if (e.getsource () == (Panel2.Button)) {Long N1, N2, F, Count = 0 ; N1 = Long. PARSELONG (Panel1.text1.gettext ()); n2 = long.parselong (panel1.text2.gettext ()); f = long.parselong (Panel1.text3.gettext ()); = n2; i ++) {if (i%f == 0) Panel2.text.append (string.valueof (i)+"");}}}}
The above is all the contents of this article. I hope everyone can like it.