In the GUI, common text boxes and text areas implement data input and output. If you use text area input, you usually set up a separate data entry completion button. When the data entry is finished, click the button. An event handler uses the GetText () method to read string information from the text area. For cases where the text box is input, the last entered return character can trigger the input completion event, usually without a separate button. Event handlers can use the Word Analyzer to analyze a number of numbers, and then use the string to convert the numerical method to obtain the input value. For output, the program converts the numeric value to a string, and then outputs the data to a text box or text area by SetText () method.
The example 11-9 small application sets a text area, a text box, and two buttons. The user enters a sequence of integers in the text area and clicks the Sum button, which sums the sequence of integers 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.
Import Java.util.*;import Java.applet.*;import java.awt.*;
Import Javax.swing.*;import java.awt.event.*; public class J509 extends Applet 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 JTextField ("", 10);
Textf.setbackground (Color.pink); B1=new JButton ("sum");
B2=new JButton ("re-start");
Textf.seteditable (FALSE); B1.addactionlistener (this);
B2.addactionlistener (this); Add (texta); Add (TEXTF);
Add (B1); add (B2);
public void actionperformed (ActionEvent e) {if (E.getsource () ==b1) {String s=texta.gettext ();
StringTokenizer tokens=new StringTokenizer (s);
Use the default delimiter collection: Spaces, wrapping, tab compliance with carriage return as delimiters int n=tokens.counttokens (), sum=0,i;
for (i=0;i<=n-1;i++) {String temp=tokens.nexttoken ();//Remove a data sum+=integer.parseint (temp) from the text area;
} textf.settext ("" +sum); } elseif (E.getsource () ==b2) {texta.settext (null);
Textf.settext (NULL);
}
}
}
The
Example 11-10 Small application calculates all numbers from the starting integer to the terminating integer that is a factor multiple. The applet container uses the GridLayout layout to divide the interface into 3 rows, the first row is the label, the second and third rows are two panel. Two panel container class Panel1,panel2 are designed and divided by GridLayout layout respectively. Panel1 is 1 rows, 6 columns, and Panel2 is 1 rows, 4 columns. The label and container class Panel1,panel2 components are then added to the corresponding position in the window.
Import Java.applet.*;import javax.swing.*;
Import Java.awt.*;import java.awt.event.*;
Class Panel1 extends jpanel{JTextField text1,text2,text3; Panel1 () {//constructor method.
When a panel object is created, the panel is initialized with three tags//three text boxes, arranged as GridLayout (1,6) text1=new JTextField (10);
Text3=new JTextField (a); SetLayout (new GridLayout (1,6));
Add (New JLabel ("Starting number", jlabel.right); add (Text1);
Add (New JLabel ("Termination number", jlabel.right)); add (Text2);
Add (New JLabel ("factor", Jlabel.right)); add (TEXT3); } class Panel2 extends jpanel{//extended Panel class JTextArea text;
JButton Button; Panel2 () {//constructor method.
When a panel object is created, the panel is initialized with a label//text box, arranged as GridLayout (1,4) text=new JTextArea (4,10); Text.setlinewrap (true);
JScrollPane jsp=new JScrollPane (text);
Button=new JButton ("Start calculation");
SetLayout (New GridLayout (1,4));
Add (new JLabel ("Calculated result:", jlabel.right));
Add (JSP);
Add (New Label ()), add (Button); } public class J510 extends Applet implements actionlistener{Panel1 Panel1; PANEL2 Panel2;
public void init () {setlayout (new GridLayout (3,1));
SetSize (400,200);p anel1=new Panel1 ();p anel2=new Panel2 ();
Add (New JLabel ("calculates number of factor multiples from start to end number", Jlabel.center));
Add (Panel1); add (Panel2); (Panel2.
Button). addActionListener (this); The 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 ());
for (long i=n1;i<=n2;i++) {if (i%f==0) panel2.text.append (string.valueof (i) + "");
}
}
}
}
The above is the entire contents of this article, I hope you can enjoy.