wesif
wesif

Reputation: 47

Java Swing GUI - Messy interface - Field is not showing up

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();

}
}

Upvotes: 1

Views: 154

Answers (1)

Chris Claude
Chris Claude

Reputation: 1372

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).

Upvotes: 3

Related Questions