JTextField em JPanel alinhamento

2 respostas
R

Já tentei de várias formas mas não consigo fazer com que os JLabel, JTextField fiquem alinhados um abaixo do outro, todos ficam um ao lado do outro. Eu gostaria que os JLabels ficassem ao lado do seu respectivo JTextField.

Tenho uma classe principal com os menus:

ApplicationWindow.java

package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class ApplicationWindow extends JFrame
{	
	private JDesktopPane desktop;

	//configura a GUI
	public ApplicationWindow()
	{
		super("titulo"); // titulo

		// Declara a barra com os menus
		JMenuBar barMenu = new JMenuBar();
		setJMenuBar(barMenu);

		// Declara os menus
		JMenu menuMenu = new JMenu("menu"); // menu
		barMenu.add(menuMenu);


		// ----------------MENU----------------------------------------------

		JMenuItem menuItem = new JMenuItem("Item");
		menuMenu.add(menuItem);


		//---------------ACOES PARA INSERIR TELAS NA AGENDA--------------------

		desktop = new JDesktopPane(); // cria o painel de area de trabalho
		desktop.setBackground(Color.lightGray);
		this.getContentPane().add(desktop);
		
		// caso o usuario clique em "Inserir Nome"
		menuItem.addActionListener(
		
				new ActionListener() // classe interna anonima
				{
					// exibe a nova janela aberta
					public void actionPerformed(ActionEvent evt)
					{
						// cria o quadro interno
						JInternalFrame frame = new JInternalFrame(
								"Titulo da janela interna", true, true, true,
								true);
						
						// cria uma tela
						Container container = frame.getContentPane();
						
						PanelPanel panelPanel =
							new PanelPanel();
						
						container.add(panelPanel, BorderLayout.CENTER);
						
						frame.pack();
						
						desktop.add(frame);
						
						frame.setVisible(true);
					}
				}
		);
	}

	public static void main (String args[])
	{
		ApplicationWindow application = new ApplicationWindow();
		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
		application.setSize( 800, 600 ); // configura o tamanho do frame
		application.setVisible(true); // exibe o frame
	}
}

E tenho uma outra classe com o JPanel e suas JLabels e JTextFields

PanelPanel.java

package ui;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PanelPanel extends JPanel
{
	private JLabel lbName;
	private JLabel lbSecondName;
	private JLabel lbNickName;
	
	private JTextField tfName;
	private JTextField tfSecondName;
	private JTextField tfNickName;
	
	/**
	 * 
	 */
	public PanelPanel()
	{		
		// label nome
		lbName = new JLabel("Nome: ");
		lbName.setLabelFor(tfName);
		lbName.setLocation(10, 10);
		add(lbName);
		
		// textfield nome
		tfName = new JTextField(30);
		add(tfName);

		
		// label sobrenome
		lbSecondName = new JLabel("Sobrenome: ");
		lbSecondName.setLabelFor(tfSecondName);
		lbSecondName.setLocation(10, 40);
		add(lbSecondName);

		// textfield sobrenome
		tfSecondName = new JTextField(30);
		add(tfSecondName);	
		
		// label apelido
		lbNickName = new JLabel("Apelido: ");
		lbName.setLabelFor(tfNickName);
		lbName.setLocation(10, 90);
		add(lbNickName);;
		
		// textfield nome
		tfNickName= new JTextField(30);
		add(tfNickName);
	}
}

Só pra constar que eu já tentei com:

lbName.setBounds(int, int, int, int);

Não estou conseguindo posicionar meus JTextFields e JLabels no JPanel

Desde já, obrigado.

2 Respostas

R

Problema resolvido

Pra quem tiver o mesmo problema, aí vai a forma como eu resolvi.

no ApplicationWindow eu mudei uma linha apenas

PanelPanel panelPanel = new PanelPanel(container);

e no PanelPanel eu fiz totalmente diferente, não ficou uma maravilha, mas resolveu meu problema

PanelPanel

package ui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PanelPanel extends JPanel
{
	/**
	 * 
	 */
	public PanelPanel( Container tela )
	{
		tela.setLayout( new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel label;
		
		JTextField textField;

		
		label = new JLabel("Label: ");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		tela.add(label, c);
		
		textField = new JTextField(20);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		tela.add(textField, c);

		label = new JLabel("Label: ");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		tela.add(label, c);
		
		textField = new JTextField(20);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		tela.add(textField, c);
	}
}

Deu pra quebra um galho, no meu programa eu criei vários labels e textField, mas como no exemplo acima, pode-se criar apenas 1 label e 1 textField.

V

Usando o GridBagLayout vc ainda tem as seguintes vantagens:

  1. Seu form pode ser redimensionado e os componentes se ajustarão;
  2. Você pode usar diferentes look&feel;
  3. Você pode usar em diferentes SO sem medo.
Criado 22 de agosto de 2008
Ultima resposta 25 de ago. de 2008
Respostas 2
Participantes 2