0

i Have a strange problem with a specific textFiled in my simple form in Java awt. My code and result look as following:

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;

public class GUI extends Frame {

public static TextField user        = null;
public static TextField password    = null;
public static TextField base        = null;

Label userLabel     = null;
Label passwordLabel = null;
Label baseLabel     = null;

public GUI(){
    this.setSize(260, 160);
    this.setTitle("Sybase connection");
    this.setResizable(false);
    //CLOSE OP
    this.addWindowListener(new WindowAdapter(){
        public void windowClosing( WindowEvent we){
            System.exit(0);
        }
    });
    //LAYOUT
    this.setLayout(null);

    // FORM PANEL
    Panel form = new Panel();
    form.setLayout(null);
    form.setBackground(new Color(0, 255, 0));
    form.setBounds(30,30, 200, 110);


    //TEXT/LABEl

    userLabel = new Label("User:");
    userLabel.setBounds(5, 5, 90, 20);
    form.add(userLabel);

    passwordLabel = new Label("Password:");
    passwordLabel.setBounds(5, 30, 90, 20);
    form.add(passwordLabel);

    baseLabel = new Label("Base:");
    baseLabel.setBounds(5, 55, 90, 20);
    form.add(baseLabel);

    user = new TextField(20);
    user.setBounds(100, 5, 90, 20);
    form.add(user);

    password = new TextField(20);
    password.setBounds(100, 30, 90, 20);
    form.add(password);

    base = new TextField(20);
    user.setBounds(100, 55, 90, 20);
    form.add(base);

    this.add(form);
    //TEXT/LABEL

    //BUTTON
    //LoginButton b = new LoginButton();

    //this.add(b);
    //BUTTON
}
public static void main(String[] args) {
    GUI frame = new GUI();
    frame.setVisible(true);
}

}

And the result: enter image description here

That ugly green background is only for check if panel is in a good place. You can see, that other two TextFields are all ok. Have you got any ideas, why the first one is not in form Panel?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Adam S
  • 9
  • 2
  • better use **netbeans** or **eclipse IDE** to Drag and drop instead of choosing random points it saves a lot of your coding time just give it a try good luck happy coding. – SmashCode Feb 16 '16 at 13:35
  • `this.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 17 '16 at 03:00
  • 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 using components in favor of Swing. 2) 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. 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Feb 17 '16 at 03:01

0 Answers0