Ver Mensaje Individual
  #4 (permalink)  
Antiguo 27/03/2012, 20:31
Avatar de SPAWN3000
SPAWN3000
 
Fecha de Ingreso: marzo-2008
Ubicación: Bogota
Mensajes: 858
Antigüedad: 16 años, 1 mes
Puntos: 15
Respuesta: Que se maximicen los componentes al maximizar ventana

BUeno ya que he visto muchos post con esta pregunta o relacionadas.. dejo un ejemplo basico.

Código:
package frametest;

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;

/**
 *
 * @author FERNANDO
 */

//Recuperar tamaños
import java.awt.Dimension;
import java.awt.Toolkit;

//Gestion de eventos
import java.awt.event.WindowEvent;

public class FrameTest extends JFrame {
    
    public JButton nn = new JButton("Aceptar");
    public static int est; 
    
	public static void main(String[] args){
		new FrameTest();
	}
        
        protected void this_windowOpened(WindowEvent e) {
                centrarVentana();
        }
        private void centrarVentana() {
                Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
                Dimension ventana = getSize();
                setLocation((pantalla.width - ventana.width) / 2,
                (pantalla.height - ventana.height) / 2);
        }
	
	public FrameTest()
	{
		super("Ejempplo!"); //titulo de ventana
                setSize(400, 400);
                this.this_windowOpened(null); //Centrar Ventana
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setLayout(null); //Deshabilitar auto resize
                
                this.add(nn); //Adicionar boton
                nn.setSize(300, 100); //Tamaño del boton
                nn.setLocation(20,20); //Ubicación del boton
		
		this.addComponentListener(new java.awt.event.ComponentAdapter() {
			public void componentResized(ComponentEvent e){
                                if(est>=2){
                                    System.out.println("JFrame was resized");
                                    // Se obtienen las dimensiones en pixels de la pantalla.
                                    Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
                                    // Se obtienen las dimensiones en pixels de la ventana.
                                    Dimension ventana = getSize();
                                    
                                    nn.setSize(ventana.width-80, nn.getSize().height);
                                    //nn.setLocation(nn.getLocation().x, nn.getLocation().y );
                                }
                                est++;
			}
		});
                
                setVisible(true);
		
	}

}
Adicional a esto es bueno leer un poco sobre flowLayout y gridLAyout....Y otro mas importante, que lo dejo a la investigacion, por que no puede ser todo...

Saludos,