1

The email field is overlaid on the name field ... and the button is not showing up.

enter image description here

I'm trying to make a simple signup screen.

package javainterfacegrafica;

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JavaInterfaceGrafica extends JFrame {

private void TelaJava(){
    Container janela = getContentPane();
    setLayout(null);

    //Definindo os rótulos
    JLabel labelUsername = new JLabel("Username: ");
    JLabel labelPassword = new JLabel("Password: ");
    JLabel labelConfirmPassword = new JLabel("Confirm Pass.: ");
    JLabel labelName = new JLabel("Name: ");
    JLabel labelEmail = new JLabel("Email: ");
    labelUsername.setBounds(50,40,100,20);
    labelPassword.setBounds(50,80,100,20);
    labelConfirmPassword.setBounds(50,120,120,20);
    labelName.setBounds(50,160,100,20);
    labelEmail.setBounds(50,160,100,20);

    JFormattedTextField jFormattedTextUsername = new JFormattedTextField();
    JFormattedTextField jFormattedTextPassword = new JFormattedTextField();
    JFormattedTextField jFormattedTextConfirmPassword = new JFormattedTextField();
    JFormattedTextField jFormattedTextName = new JFormattedTextField();
    JFormattedTextField jFormattedTextEmail = new JFormattedTextField();
    jFormattedTextUsername.setBounds(150,40,100,20);
    jFormattedTextPassword.setBounds(150,80,100,20);
    jFormattedTextConfirmPassword.setBounds(150,120,100,20);
    jFormattedTextName.setBounds(150,160,180,20);
    jFormattedTextEmail.setBounds(150,160,180,20);


    //Botão
    JButton btn = new JButton("Salvar");


    //Adiciona os rótulos e os campos de textos com máscaras na tela
    janela.add(labelUsername);
    janela.add(labelPassword);
    janela.add(labelConfirmPassword);
    janela.add(labelName);
    janela.add(labelEmail);
    janela.add(jFormattedTextUsername);
    janela.add(jFormattedTextPassword);
    janela.add(jFormattedTextConfirmPassword);
    janela.add(jFormattedTextName);
    janela.add(jFormattedTextEmail);
    janela.add(btn);

    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}


public static void main(String[] args) {

    JavaInterfaceGrafica jig = new JavaInterfaceGrafica();``
    jig.TelaJava();

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wesif
  • 47
  • 6
  • 2
    1) Quit repeating the same sentence in the description of your problem. 2) Don't use null layout and setBounds(). Managing the bounds will cause you problems (as you have noticed), since not all your values are unique and you didn't specify a bounds for the button. 3) Instead you should be using `Layout Managers`. Swing was designed to be used with layout managers. Read the section from the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). I would suggest you could use a `GridBagLayout` to display the components in a grid like fashion. – camickr Jul 06 '19 at 15:28

1 Answers1

3

Your email layout is overlaid because of the following labelName.setBounds(50,160,100,20); labelEmail.setBounds(50,160,100,20); you're basically giving them the same location on your frame. For your button try giving it a location in your layout using setBounds(x, x, x, x);. Also, for a simple login layout like yours, I'd advise using GridLayout with 2 columns and 5 rows (the fifth one for your login button).

Chris Claude
  • 973
  • 12
  • 25