Search code examples
javauser-interfacesetbounds

java gui setbounds doesnt work


i am trying to make an UI in java and i am new in this so sorry if it's an easy question.

public class viewDeneme extends JFrame {

private static final long serialVersionUID = -7284396337557548747L;
private JTextField nameTxt = new JTextField(10);
private JTextField passwordTxt = new JTextField(10);
private JButton loginBtn = new JButton("Giriş");
private JLabel nameLbl = new JLabel("Kullanıcı adi:");
private JLabel passwordLbl = new JLabel("Şifre:");

public viewDeneme(){
    JPanel loginPanel = new JPanel();
    this.setSize(600,200);
    this.setLocation(600, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    nameLbl.setBounds(200, 200, 100, 50);

    loginPanel.add(nameTxt);
    loginPanel.add(passwordTxt);
    loginPanel.add(loginBtn);
    loginPanel.add(nameLbl);
    loginPanel.add(passwordLbl);

    this.setVisible(true);
    this.add(loginPanel);
}

public static void main(String[] args) {
    new viewDeneme();

}
}

here is my code.I am trying to set bounds for labels and textboxes but it's not changing anything.There isn't any error so i must missing something but i couldn't find it with searching in web.Thanks for your help


Solution

  • See What is setBounds and how do I use it?

    JPanel layout must be null to use absolute positioning. JPanel object is initialized to use a FlowLayout, unless you specify differently when creating the JPanel. So you must write

    loginPanel.setLayout(null);