Utilizar JFrame sem o uso do extends

7 respostas
F

Olá pessoal, quando executo esta aplicação o JTextArea fica muito pequeno dentro do panel, aumentei o tamanho mas não tive sucesso.

O que deve estar acontecendo ? … Estou Utilizando corretamente o JFrame, JPanel e o JTextArea junto com JScrollPane ?

package exercicio2_chat_jframe;

import javax.swing.*;

public class TestesGUIs {

    private JFrame frame;
    private JPanel panelDialog;
    private JPanel panelMensag;
    private JTextArea dialogTextArea;
    private JTextArea mensageTextArea;
    private JButton enviarButton;
    private JScrollPane scrollDialog;


    public TestesGUIs(){
        
        dialogTextArea = new JTextArea();
       
        scrollDialog = new JScrollPane(dialogTextArea);
        scrollDialog.setBounds(20,20, 200,200);

        panelDialog = new JPanel();
        panelDialog.setLayout(null);
        panelDialog.setSize(400,400);
        panelDialog.setVisible(true);
        
        panelDialog.add(scrollDialog);

        frame = new JFrame("Cliente");
        frame.setLayout(null);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setLocation(100,100);

        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(frame);
            SwingUtilities.updateComponentTreeUI(panelDialog);
        }catch (Exception e){}

        frame.getContentPane().add(panelDialog);
    }

    public static void main(String args[]){
        new TestesGUIs();
    }

}

7 Respostas

ViniGodoy

Defina o layout do seu panelDialog para null também.

Agora, ao invés de usar null em todos os layouts, o ideal mesmo seria você aprender a trabalhar com os layout managers. O null não deixa a aplicação multiplataforma, e nem permite que você redimensione a aplicação depois.

tkx

Mude um trecho do seu código! Ao invés de adicionar o textarea no scroll, crie um scroll com textarea (construtor).
Troque isso:

scrollDialog = new JScrollPane(); scrollDialog.add(dialogTextArea);
Por isso:

scrollDialog = new JScrollPane(dialogTextArea);

F

Efetuei as duas recomendações dos camaradas mas… só está me aparecendo agora o panel e o frame… o textarea com o scroll não está aparecendo…

package exercicio2_chat_jframe;

import javax.swing.*;

public class TestesGUIs {

    private JFrame frame;
    private JPanel panelDialog;
    private JPanel panelMensag;
    private JTextArea dialogTextArea;
    private JTextArea mensageTextArea;
    private JButton enviarButton;
    private JScrollPane scrollDialog;


    public TestesGUIs(){
        
        dialogTextArea = new JTextArea(20,20);
       
        scrollDialog = new JScrollPane(dialogTextArea);
        //scrollDialog.add(dialogTextArea);
        scrollDialog.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollDialog.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

        panelDialog = new JPanel();
        panelDialog.setLayout(null);
        panelDialog.setSize(400,400);
        panelDialog.setVisible(true);
        

        panelDialog.add(scrollDialog);

        frame = new JFrame("Cliente");
        frame.setLayout(null);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setLocation(100,100);

        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(frame);
        }catch (Exception e){}

        frame.getContentPane().add(panelDialog);
    }

    public static void main(String args[]){
        new TestesGUIs();
    }

}
ViniGodoy

Faltou definir o tamanho do Scroll.

F

Muito Obrigado aos camaradas. Modifiquei algumas outras coisas e está pegando!

package exercicio2_chat_jframe;

import javax.swing.*;

public class TestesGUIs {

    private JFrame frame;
    private JPanel panelDialog;
    private JPanel panelMensag;
    private JTextArea dialogTextArea;
    private JTextArea mensageTextArea;
    private JButton enviarButton;
    private JScrollPane scrollDialog;


    public TestesGUIs(){
        
        dialogTextArea = new JTextArea();
       
        scrollDialog = new JScrollPane(dialogTextArea);
        scrollDialog.setBounds(20,20, 200,200);

        panelDialog = new JPanel();
        panelDialog.setLayout(null);
        panelDialog.setSize(400,400);
        panelDialog.setVisible(true);
        
        panelDialog.add(scrollDialog);

        frame = new JFrame("Cliente");
        frame.setLayout(null);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setLocation(100,100);

        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(frame);
            SwingUtilities.updateComponentTreeUI(panelDialog);
        }catch (Exception e){}

        frame.getContentPane().add(panelDialog);
    }

    public static void main(String args[]){
        new TestesGUIs();
    }

}
F

ViniGodoy:
Defina o layout do seu panelDialog para null também.

Agora, ao invés de usar null em todos os layouts, o ideal mesmo seria você aprender a trabalhar com os layout managers. O null não deixa a aplicação multiplataforma, e nem permite que você redimensione a aplicação depois.

Ola camarada, voce conhece algum link ou post sobre esse assunto ?

Muito Obrigado!

ViniGodoy

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Criado 23 de março de 2010
Ultima resposta 23 de mar. de 2010
Respostas 7
Participantes 3