Problema em mostrar label

2 respostas
T

Olá.

Estou querendo mostrar na minha frame dois labels diferentes, só que o valor do último atributo sempre apaga o 1º. Já tentei posicionar para opostos mas não funciona.
PS.: Ainda tenho problemas com a estrutura da linguagem, então meu código pode estar todo bagunçado.

JLabel label1 = new JLabel("Hello, Swing World", SwingConstants.RIGHT);
        JLabel label2 = new JLabel("Good morning!", SwingConstants.LEFT);

        JFrame frame = new JFrame("Juliana");
        frame.getContentPane().add(label1);
        frame.getContentPane().add(label2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

Espero que possam me corrigir e me ajudar :wink:

2 Respostas

brunoccouto

Configura um gerenciador de Layout para o seu frame.
Esse link tem tudo sobre o assunto: [url]http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html[/url]

Usando GridLayout no seu exemplo:

JLabel label1 = new JLabel("Hello, Swing World");
JLabel label2 = new JLabel("Good morning!");

JFrame frame = new JFrame("Juliana");
frame.setLayout(new GridLayout(2, 1));

frame.add(label1);
frame.add(label2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
T

Obrigada!!! :wink:

Criado 19 de fevereiro de 2009
Ultima resposta 19 de fev. de 2009
Respostas 2
Participantes 2