Java Swing GUI - Messy interface - Field not showing

Asked

Viewed 51 times

0

inserir a descrição da imagem aqui

The email field is superimposed over the name field... and the button is not showing.

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

    //Seta as máscaras nos objetos JFormattedTextField
    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(400, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}


public static void main(String[] args) {

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

}
}
  • I read in Soen that "setLayout(null) never ends well" and I fear there is truth in that statement. I suggest practicing with other layouts, in particular those I know GridBagLayout Sounds good to me. The only problem with it is that it depends on a visual editor, and you haven’t reported whether you’re using Netbeans (which already has one) or Eclipse (which has for example Window Builder) or even some other one. I suggest you search which editor fits your situation and try to do with the GridBagLayout, that once you get the hang of it is very simple.

1 answer

0


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,200,100,20);

    //Seta as máscaras nos objetos JFormattedTextField
    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,200,180,20);


    //Botão
    JButton btn = new JButton("Salvar");
    btn.setBounds(50,240,100,20);


    //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(400, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}


public static void main(String[] args) {

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

}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.