Hello Readers, in this article we will learn to create input box and give input from user as string and JAVA-print them in Applet.
The code shown above is a Java program that creates an applet that prompts the user to enter five words, which are then concatenated into a single string and displayed on the screen.
The appletString class extends the Applet class, which provides a framework for building graphical user interfaces. The class contains five TextField objects, which are used to capture user input. These text fields are initialized in the init() method, which is called when the applet is first created. The add() method is used to add the text fields to the applet’s graphical user interface.
The paint() method is responsible for displaying the applet’s output on the screen. It starts by displaying the message “Enter Your Word” at the top of the screen. The method then reads the user’s input from each of the text fields using the getText() method. These strings are concatenated together into a single string using the + operator and stored in the variable s. Finally, the method displays the concatenated string at position (20,200) on the screen using the drawString() method.
The action() method is called when the user interacts with the applet. In this case, it simply calls the repaint() method to redraw the applet’s output on the screen.
Overall, this program is a simple example of how to build a graphical user interface in Java using the Applet class. It demonstrates how to create text fields, read user input, and display output on the screen. However, it should be noted that the Applet class is no longer recommended for building user interfaces in Java, as it has been deprecated in favor of newer technologies such as JavaFX and Swing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package appletstring; import java.applet.*; import java.awt.*; public class appletString extends Applet { TextField text1, text2, text3, text4, text5; public void init() { text1 = new TextField(8); text2 = new TextField(8); text3 = new TextField(8); text4 = new TextField(8); text5 = new TextField(8); add(text1); add(text2); add(text3); add(text4); add(text5); } public void paint(Graphics g) { String s1,s2,s3,s4,s5; String s=""; g.drawString("Enter Your Word ",0,10); try { s1 = text1.getText(); s2 = text2.getText(); s3 = text3.getText(); s4 = text4.getText(); s5 = text5.getText(); s = s1 + " " +s2 + " " + s3 + " " +s4 + " " +s5; } catch(Exception e) {} g.drawString(s,20,200); } public boolean action(Event event, Object obj) { repaint(); return true; } } |
Hope this article will be helpful for you. Please read our another tutorial here.