2

My Applet with text field
I have this applet (above is the image) with 2 text field. How do I separate these text fields to different lines?

code

import java.applet.*;
import java.awt.*;

public class Print extends Applet {
    TextField text1,text2;
    public void init(){ 
        text1=new TextField(8);
        text2=new TextField(8);  
        add(text1);
        add(text2);

    }
   public void paint(Graphics g) { 
      g.drawString("Welcome in Java Applet.",40,200);
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Shibin Raju Mathew
  • 920
  • 4
  • 18
  • 41
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 4) The answer is to .. – Andrew Thompson May 14 '17 at 00:35
  • .. be found in 'layouts'. Choose an appropriate one or combine them as needed. To have the text fields in two rows you might look to `GridLayout`, `GridBagLayout`, `BoxLayout`.. I would recommend dumping the custom painting and instead adding a `Label` (if you stick with AWT) or (preferably) a `JLabel`. – Andrew Thompson May 14 '17 at 00:35

2 Answers2

1

The simplest method is using setBounds(x,y,width,height). In your case use code as below

text1.setBounds(10,100,100,20);
text2.setBounds(10,130,100,20);
Drench
  • 141
  • 1
  • 1
  • 5
0

You can increase the size of TextField and decrease the size of panel, it will automatically come in next line

I increased the size of textField from 10 to 20 and it automatically comes in next line

Marshall
  • 69
  • 1
  • 4