Layout no JPanel

3 respostas
jmendes

Olá pessoal...
Estou com problemas...Não consigo formatar o tamanho dos botões no meu layout. Olhem o código abaixo:

import javax.swing.*; 
import java.awt.*;

public class abas extends JFrame { 
	
	private JTabbedPane tabs = new JTabbedPane() ; 
	private JPanel painel1, painel2;


	public abas () { 
		super("teste");
		
		//aba 1  
		painel1 = new JPanel();
		painel1.setLayout(new GridLayout(2,2));
		JLabel texto = new JLabel("Texto: ");
		painel1.add(texto);
		JButton botao = new JButton("Botão: ");
		botao.setSize(10,10);
		painel1.add(botao);
		JLabel textoe = new JLabel("Texto: ");
		painel1.add(textoe);
		JButton botaoe = new JButton("Botão: ");
		painel1.add(botaoe);
		tabs.addTab("Aba 1",painel1);
		getContentPane().add(tabs); 
		
		//aba 2
		painel2 = new JPanel();
		JLabel texto2 = new JLabel("Texto 2:"); 
		painel2.add(texto2);
		JButton botao2 = new JButton("Botão 2: ");
		painel2.add(botao2);
		tabs.addTab("Aba 2", painel2); 
		getContentPane().add(tabs); 
		
		setBounds(100, 100, 300, 300);
		show();
		} 


	public static void main(String args[]) { 
		abas telaabas = new abas();
		telaabas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//FECHANDO O PROGRAMA 
		} 

	}

Por favor..me ajudem!!!

3 Respostas

dsfextreme
jmendes:
Olá pessoal... Estou com problemas...Não consigo formatar o tamanho dos botões no meu layout. Olhem o código abaixo:
import javax.swing.*; 
import java.awt.*;

public class abas extends JFrame { 
	
	private JTabbedPane tabs = new JTabbedPane() ; 
	private JPanel painel1, painel2;


	public abas () { 
		super("teste");
		
		//aba 1  
		painel1 = new JPanel();
		painel1.setLayout(new GridLayout(2,2));
		JLabel texto = new JLabel("Texto: ");
		painel1.add(texto);
		JButton botao = new JButton("Botão: ");
		botao.setSize(10,10);
		painel1.add(botao);
		JLabel textoe = new JLabel("Texto: ");
		painel1.add(textoe);
		JButton botaoe = new JButton("Botão: ");
		painel1.add(botaoe);
		tabs.addTab("Aba 1",painel1);
		getContentPane().add(tabs); 
		
		//aba 2
		painel2 = new JPanel();
		JLabel texto2 = new JLabel("Texto 2:"); 
		painel2.add(texto2);
		JButton botao2 = new JButton("Botão 2: ");
		painel2.add(botao2);
		tabs.addTab("Aba 2", painel2); 
		getContentPane().add(tabs); 
		
		setBounds(100, 100, 300, 300);
		show();
		} 


	public static void main(String args[]) { 
		abas telaabas = new abas();
		telaabas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//FECHANDO O PROGRAMA 
		} 

	}

Por favor..me ajudem!!!

Vc está usando o que para fazer sua janela , na unha ou alguma ide ?

dsfextreme

Fiz uma tela parecida pra vc ,olhe se ficou legal.
Se vc não usa nenhuma IDE ,recomendo usar o NetBeans novo ,que tá matando a pau em relação a desenvolvimento de telas com o Matisse.
Vale a pena conferir.
Espero que sirva o código,fiz no Eclipse com o VE ....
Um Abraço!

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.JButton;

public class NovaAba extends JFrame {

	private JPanel jContentPane = null;
	private JTabbedPane jTabbedPane = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField1 = null;
	private JButton jButton1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField2 = null;
	private JButton jButton2 = null;

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("Aba 1", null, getJPanel(), null);
			jTabbedPane.addTab("Aba 2", null, getJPanel1(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.gridy = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 1;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Texto 2 :");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Texto 1 :");
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(jLabel, gridBagConstraints);
			jPanel.add(getJTextField(), gridBagConstraints1);
			jPanel.add(getJButton(), gridBagConstraints2);
			jPanel.add(jLabel1, gridBagConstraints3);
			jPanel.add(getJTextField1(), gridBagConstraints4);
			jPanel.add(getJButton1(), gridBagConstraints5);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 2;
			gridBagConstraints8.gridy = 0;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.gridy = 0;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 0;
			jLabel2 = new JLabel();
			jLabel2.setText("Texto 3 aba 2");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(jLabel2, gridBagConstraints6);
			jPanel1.add(getJTextField2(), gridBagConstraints7);
			jPanel1.add(getJButton2(), gridBagConstraints8);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("click 1");
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Click 2");
		}
		return jButton1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
		}
		return jTextField2;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Click 2 aba 2");
		}
		return jButton2;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NovaAba abas = new NovaAba();
		abas.setVisible(true);

	}

	/**
	 * This is the default constructor
	 */
	public NovaAba() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

}

Estou anexando uma figura de uma aplicação que estou fazendo no NetBeans pra vc ver como fica uma janela....
[IMG]http://Serv1.imagehigh.com/files/ih000001/31727_telaExemplo.th.JPG[/IMG]

jmendes

Valeu dsfextreme…vai me ajudar bastante essa sua aplicação!
Tou fazendo tudo na unha (eclipse 2.1), não faço no NetBeans por causa do meu PC, q tá bem ultrapassado…
Obrigado!

Criado 7 de maio de 2006
Ultima resposta 7 de mai. de 2006
Respostas 3
Participantes 2