Ver Mensaje Individual
  #6 (permalink)  
Antiguo 16/03/2012, 07:34
Avatar de caruizj
caruizj
 
Fecha de Ingreso: marzo-2012
Ubicación: Málaga
Mensajes: 161
Antigüedad: 12 años, 2 meses
Puntos: 14
Respuesta: Interfaz en swing

Bueno te cuento...

VentanaPrincipal es JFRAME es la ventana principal.

En la ventana principal se alojan 3 JInternalFrames, Izquierdo, Superior y central.

Ahí va el código:

VENTANA PRINCIPAL (JFRAME):


Código:
public class VentanaPrincipal extends JFrame {	



	private VentanaPrincipal vp;
	private PanelCentral pc;
	private PanelSuperior ps;
	private PanelIzquierdo pi;	
	
	
	public VentanaPrincipal(){		
		
		this.setLayout(null);
		this.setTitle("VENTANA PRINCIPAL");
		this.setResizable(true);
		this.setBounds(0, 0, 1024, 768);
		this.setLocationRelativeTo(null);
		vp=this;			   
	    	
		
				
		
		
		
		
		/******** PANEL CENTRAL *******************/
		 pc = new PanelCentral();
		
		
		
		/******** PANEL SUPERIOR *******************/
		 ps = new PanelSuperior(this, pc);
		
		
		/******** PANEL IZQUIERDO *******************/
		 pi = new PanelIzquierdo(this,ps,pc);
		

		 ps.setPanelIzquierdo(pi);

		
		/******** AÑADIR A LA VENTANA *******************/
		add(pi);		
		add(pc);			
		add(ps);

		
	
				
		/******** ESCUCHADOR CUANDO CAMBIAMOS TAMAÑO DE VENTANA PRINCIPAL *******************/
		this.addComponentListener(new ComponentAdapter() {
                  public void componentResized(ComponentEvent e) {
                
                    pi.setBounds(0, 30, 200, vp.getHeight()-72);
                    pc.setBounds(pi.getWidth(),ps.getHeight()+30,vp.getWidth()-pi.getWidth()-18,vp.getHeight()-ps.getHeight()-72);
                    ps.setBounds(pi.getWidth(),30,vp.getWidth()-pi.getWidth()-18,vp.getHeight()-pc.getHeight()-70);
                
                  
            }
        });	
		
		
	}	
	
    public static String getFechaActual() {
        Date ahora = new Date();
        SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy");
        return formateador.format(ahora);
    }

   
    public static String getHoraActual() {
        Date ahora = new Date();
        SimpleDateFormat formateador = new SimpleDateFormat("hh:mm:ss");
        return formateador.format(ahora);
    }
	
	
	
}
PANEL CENTRAL:

Código:
public class PanelCentral extends JInternalFrame  {


	private int x, y;
	private PanelCentral pc;	
	private JScrollPane scrollPane;
	
	public PanelCentral(){
		
		pc=this;
		this.setLayout(null);
		this.setTitle(null);
		this.setVisible(true);
		this.setClosable(false);
		this.setMaximizable(false);
		this.setResizable(false);
			
			
	}

}
PANEL SUPERIOR
Código:
public class PanelSuperior extends JInternalFrame implements ComponentListener {

	private PanelIzquierdo vpi;
	private VentanaPrincipal vvp;
	private PanelSuperior vps;
	private PanelCentral vpc;
		
	public PanelSuperior(VentanaPrincipal vp, PanelCentral vc){
		
		vps=this;
		vvp=vp;
		vpc=vc;
				
		
		this.setLayout(null);
		this.setTitle(null);
		this.setVisible(true);
		this.setClosable(false);
		this.setMaximizable(false);
		this.setResizable(true);
		
		this.addComponentListener(this);
		
		
	
		
		this.addComponentListener(new ComponentAdapter() {
                    public void componentResized(ComponentEvent e) {
                        
            	
            	 vpc.setBounds(vpi.getWidth(),vps.getHeight()+30,vvp.getWidth()-vpi.getWidth()-18,vvp.getHeight()-             vps.getHeight()-72);
            }
        });
		
	}
	
	public void setPanelIzquierdo(PanelIzquierdo pi){
		
		this.vpi=pi;
	}

	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		vps.reshape(vpi.getWidth(), 30,vvp.getWidth()-vpi.getWidth()-18,vvp.getHeight()-vpc.getHeight()-70);
				
	}

	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	
		
	
}

PANEL IZQUIERDO

Código:
public class PanelIzquierdo extends JInternalFrame implements ComponentListener , MouseListener{


	private PanelIzquierdo pi;
	private VentanaPrincipal vvp;
	private PanelSuperior vps;
	private PanelCentral vpc;
	private JTree tree;

public PanelIzquierdo(VentanaPrincipal vp, PanelSuperior vs, PanelCentral vc){
	
	
	//this.setLayout(null);
	this.setBounds(0,30,200,768);	
	this.setVisible(true);
	this.setClosable(false);
	this.setMaximizable(false);
	this.setResizable(true);		
	this.setTitle("Navegador");
	
	
	
	pi=this;
	vvp=vp;
	vps=vs;
	vpc=vc;
	
	this.addComponentListener(this);
	this.addMouseListener(this);
	
	crearJTREE();
		
	
	this.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
                   
            vpc.setBounds(pi.getWidth(),260,vvp.getWidth()-pi.getWidth()-18,vvp.getHeight()-302);
            vps.setBounds(pi.getWidth(),30,vvp.getWidth()-pi.getWidth()-18,vvp.getHeight()-vpc.getHeight()-70);
            tree.setBounds(0, 0, pi.getWidth(), pi.getHeight());
        }
    });
	
}

@Override
public void componentHidden(ComponentEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void componentMoved(ComponentEvent e) {
	pi.reshape(0, 30, 200, vvp.getHeight()-72);
	
}

@Override
public void componentResized(ComponentEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void componentShown(ComponentEvent e) {
	// TODO Auto-generated method stub
	
}

public void crearJTREE(){
	
	DefaultMutableTreeNode Sistema = new DefaultMutableTreeNode("Sistema");
	DefaultMutableTreeNode hijos = new DefaultMutableTreeNode("hijos");
	DefaultTreeModel modelo = new DefaultTreeModel(Sistema);
	tree = new JTree(modelo);
	
	tree.setBounds(0, 0, pi.getWidth(), pi.getHeight());

	
	
	DefaultMutableTreeNode Medicos = new DefaultMutableTreeNode("Medicos");
	DefaultMutableTreeNode Otros = new DefaultMutableTreeNode("Otros");
	DefaultMutableTreeNode Altas=new DefaultMutableTreeNode("Altas");
	DefaultMutableTreeNode Bajas=new DefaultMutableTreeNode("Bajas");
	DefaultMutableTreeNode Quirofano = new DefaultMutableTreeNode("Quirofano");
	DefaultMutableTreeNode Otros2=new DefaultMutableTreeNode("Otros 2");
	
	
	modelo.insertNodeInto(Medicos,Sistema,0);
	modelo.insertNodeInto(Otros, Sistema, 1);
	modelo.insertNodeInto(Otros2, Sistema, 2);
	modelo.insertNodeInto(Altas, Medicos, 0);
	modelo.insertNodeInto(Bajas, Medicos, 1);
	modelo.insertNodeInto(Quirofano, Medicos, 2);

		
	add(tree);
}

@Override
public void mouseClicked(MouseEvent e) {

	
}

@Override
public void mouseEntered(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void mouseExited(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {
	// TODO Auto-generated method stub
	
}


}

Bueno llamariamos desde main a VentanaPrincipal.

La estructura queda así:


h ttp://www.hiboox.es/go/imagenes/adultos/estructura,573d388f46043d3c76714852aaa20d45.png.ht ml


Lo que no sé es como va el escuchador del JTree para que cuando le hagas click sobre un nodo se escuche... ¿Qué es haciendo un addMouseListener? ¿lo sabes?

Un saludo.
__________________
Aprendiendo un poco de Java :D