Layout JTextField

5 respostas
C

Prezados, estou encontrando dificuldade em limitar o tamanho o JTextField.
Vejam defini o tamanho dos JTextField como tendo 5 colunas, mas ele assume o tamanho maximo da posição no Grid.
Como posso resolver teste problema? Já tentei alterar o Layout do Jpanel para todos os possíveis e não obtive sucesso.
Necessito de um linha contendo os 6 JTextField, lado a lado.

Agradeço a todos...

Trajano
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.*;

public class Teste extends JFrame implements ActionListener {

	// Criação das TextAreas, 
	private JTextArea jTestatistica;
	private JTextArea jThistorico;
	private String[] vhistoricos = null;	

	public Teste(String title) {
		super(title);

		jTestatistica = new JTextArea(10, 45);
		jTestatistica.setEditable(false);

		jThistorico = new JTextArea(20, 45);
		jThistorico.setEditable(false);
	}

	private void criaMostraGUI() {

		// Definição do comportamento e Layout da janela
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		setLocation(300, 300);		
		
		// Criação dos Painéis e Definição dos Layouts
		JPanel jPestatistica = new JPanel();
		jPestatistica.setLayout(new BorderLayout());
		JPanel jPhistorico = new JPanel();
		jPhistorico.setLayout(new BorderLayout());
		JPanel jPnovoConcurso = new JPanel();
		jPnovoConcurso.setLayout(new GridLayout(1,6));
		
		//Criação dos JTextFields
		JTextField jTnovoConcurso = new JTextField(5);
		JTextField jTdez1 = new JTextField(5);
		JTextField jTdez2 = new JTextField(5);
		JTextField jTdez3 = new JTextField(5);
		JTextField jTdez4 = new JTextField(5);
		JTextField jTdez5 = new JTextField(5);		
		
		
		// Adicionando JScrollPane aos JTextArea
		JScrollPane sPestatistica = new JScrollPane(jTestatistica);
		JScrollPane sPhistorico = new JScrollPane(jThistorico);

		// Adicionando os JScrollPane aos Painéis
		jPestatistica.add("Center", sPestatistica);
		jPhistorico.add("Center", sPhistorico);
		
		//Adiciona ao Painél jPnovoConcurso os JTextField		
		JLabel t = new JLabel("");		
		jPnovoConcurso.add(jTnovoConcurso);
		jPnovoConcurso.add(jTdez1);
		jPnovoConcurso.add(jTdez2);
		jPnovoConcurso.add(jTdez3);
		jPnovoConcurso.add(jTdez4);
		jPnovoConcurso.add(jTdez5);
		
		

		// Adicionando os Painéis ao JFrame, seguindo o Layout definido
		getContentPane().add("North", jPestatistica);
		getContentPane().add("Center", jPhistorico);
		getContentPane().add("South", jPnovoConcurso);
				
		// Comportamento da janela
		pack();
		setVisible(true);
		// Sempre visivel
		setResizable(false);
		// Janela de tamanho fixo
	}

	public static void main(String[] args) {
		Teste gui = new Teste("Teste V1.0");
		gui.criaMostraGUI();
	}

	// Tratamento dos eventos do MOUSE
	public void actionPerformed(ActionEvent evt) {
		// if (evt.getSource() == ) {// evento do botão sair
		// System.exit(0);
	}

	// Tratamento dos eventos do TECLADO
	public void teclado_keyTyped(KeyEvent e) {

	}

}

5 Respostas

fec

Cara deve ser pelo layout BorderLayout, tenta usar em um dos painéis, que você quer adicionar esse mesmo textfield,um layout diferente tipo GridBagLayout.

Valew!!!

C

Valeu, mas já estou utilizando o Layout do Paneil como Grid…
jPnovoConcurso.setLayout(new GridLayout(1,6));

fec

Aê amigo, é o GridBagLayout!
Segue um pequeno tutorial dele:
http://www.javafree.org/javabb/viewtopic.jbb?t=5792

Se tiver usando o Netbeans, aí é uma “mão na roda”.
Valew!

A

conectionsp:
Valeu, mas já estou utilizando o Layout do Paneil como Grid…
jPnovoConcurso.setLayout(new GridLayout(1,6));

A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size.
http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html

Portanto, o GridLyout não respeita o tamanho dos componentes.

C

Agradeço a todos, agora só estudar e implementar…

Criado 8 de novembro de 2007
Ultima resposta 8 de nov. de 2007
Respostas 5
Participantes 3