0

I have a public class which extends from JFrame. Inside this class I Have a JPanel, which I'm adding fields and buttons. But the problems is that no matter if I set .setLocation, Java will ignore it and my components are not being alligned as I pretend.

public class FrmLogin extends JFrame {

    public FrmLogin() {
        setTitle("Login panel");
        setFont(new Font("Source Code Pro", Font.PLAIN, 16));
        setSize(640, 480);
        setLocation(650, 250);

        // Usuario
        JLabel usuarioLbl = new JLabel("Usuario:");
        usuarioLbl.setFont(new Font("Source Code Pro", Font.PLAIN, 16));
        usuarioLbl.setLocation(320,100);

        JTextField usuarioFld = new JTextField();
        usuarioFld.setFont(new Font("Source Code Pro", Font.PLAIN, 16));
        usuarioFld.setHorizontalAlignment(SwingConstants.CENTER);
        usuarioFld.setLocation(320,120);
        usuarioFld.setColumns(24);

        // Passowrd
        JLabel passwordLbl = new JLabel("Password:");
        passwordLbl.setFont(new Font("Source Code Pro", Font.PLAIN, 16));
        passwordLbl.setLocation(320,140);

        JPasswordField passwordFld = new JPasswordField();
        passwordFld.setFont(new Font("Source Code Pro", Font.PLAIN, 16));
        passwordFld.setHorizontalAlignment(SwingConstants.CENTER);
        passwordFld.setLocation(320,160);
        passwordFld.setColumns(24);

        //Botón
        JButton loginBtn = new JButton("Ingresar");
        loginBtn.setFont(new Font("Source Code Pro", Font.PLAIN, 16));
        loginBtn.setHorizontalAlignment(SwingConstants.CENTER);
        setLocation(320,180);
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tryLogin(usuarioFld.getText(), passwordFld.getPassword());
            }
        });

        JPanel loginPnl = new JPanel();

        loginPnl.setLocation(0,0);
        loginPnl.setSize(660,440);
        loginPnl.add(usuarioFld);
        loginPnl.add(usuarioLbl);
        loginPnl.add(passwordFld);
        loginPnl.add(passwordLbl);
        loginPnl.add(loginBtn);

        setLayout(null);
        setContentPane(loginPnl);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public void appear() {
        this.setVisible(true);
    }


    public void destroy() {
        this.setVisible(false);
        this.destroy();
    }
}

The components just appear one next to each other.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gonzalo Dambra
  • 891
  • 2
  • 19
  • 34
  • 1
    You're forgetting about the layout manager for the loginPnl which holds your components and which is FlowLayout by default; this layout ignores all contained component's location and size properties – Hovercraft Full Of Eels Dec 08 '19 at 21:46
  • 1
    But having said this, you will be painting yourself into a frustrating and hard to fix corner by using null layouts and `setLocation(...)` or `setBounds(...)` for component placement as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. – Hovercraft Full Of Eels Dec 08 '19 at 21:47

0 Answers0