t_java 18 de jan. de 2008
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
public class JanelaPrincipal extends JFrame {
private JanelaMapa mapa ;
private Point coordenada ;
private Dimension tamanho ;
private boolean executar = false ;
private boolean processo = false ;
private boolean exibido = true ;
private JButton cmdexibir ;
public JanelaPrincipal () {
super ();
setSize ( 500 , 500 );
setLayout ( null );
mapa = new JanelaMapa ();
cmdexibir = new JButton ( "Esconder Mapa" );
cmdexibir . setBounds ( 100 , 100 , 250 , 250 );
getContentPane (). add ( cmdexibir );
setVisible ( true );
setDefaultCloseOperation ( EXIT_ON_CLOSE );
mapa . setVisible ( true );
moverJanelaMapa ();
addComponentListener ( new ComponentAdapter () {
public void componentMoved ( ComponentEvent e ) {
if ( exibido ) {
moverJanelaMapa ();
}
}
public void componentResized ( ComponentEvent e ){
if ( exibido ) {
redimensionarJanela ();
}
}
});
addWindowListener ( new WindowAdapter () {
public void windowActivated ( WindowEvent e ) {
if ( exibido ) {
if ( executar ) {
processo = true ;
focarMapa ();
}
}
}
public void windowDeactivated ( WindowEvent e ) {
if ( exibido ) {
if ( ! processo ) {
executar = true ;
}
processo = false ;
}
}
public void windowClosed ( WindowEvent e ) {
mapa . dispose ();
}
public void windowClosing ( WindowEvent e ) {
mapa . dispose ();
}
});
mapa . addComponentListener ( new ComponentAdapter () {
public void componentMoved ( ComponentEvent e ) {
moverJanelaPrincipal ();
}
});
cmdexibir . addActionListener ( new ActionListener () {
public void actionPerformed ( ActionEvent e ) {
if ( exibido ) {
cmdexibir . setText ( "Mostrar Mapa" );
exibido = false ;
mapa . setVisible ( false );
} else {
cmdexibir . setText ( "Esconder Mapa" );
exibido = true ;
mapa . setVisible ( true );
moverJanelaPrincipal ();
redimensionarJanela ();
}
}
});
toFront ();
}
private void moverJanelaMapa () {
coordenada = getLocation ();
tamanho = getSize ();
mapa . setLocation (( int ) ( coordenada . getX () + tamanho . getWidth ()),( int ) ( coordenada . getY ()));
}
private void moverJanelaPrincipal () {
coordenada = mapa . getLocation ();
tamanho = getSize ();
setLocation (( int ) ( coordenada . getX () - tamanho . getWidth ()),( int ) ( coordenada . getY ()));
}
private void redimensionarJanela (){
Dimension tela = Toolkit . getDefaultToolkit (). getScreenSize ();
tamanho = getSize ();
if ( tela . getWidth () < ( tamanho . getWidth () + 50 )) {
tamanho . setSize (( int ) tamanho . getHeight (),( int ) ( tamanho . getWidth () - 50 ));
setSize (( int ) tamanho . getHeight (),( int ) tamanho . getWidth ());
}
mapa . setSize ( 50 ,( int ) tamanho . getHeight ());
moverJanelaMapa ();
}
private void focarMapa () {
executar = false ;
mapa . toFront ();
toFront ();
}
public static void main ( String [] args ){
new JanelaPrincipal ();
}
}
class JanelaMapa extends JDialog {
public JanelaMapa () {
super ();
setSize ( 50 , 500 );
setResizable ( false );
setDefaultCloseOperation ( DO_NOTHING_ON_CLOSE );
setLayout ( new GridLayout ( 0 , 2 ));
getContentPane (). add ( new JLabel ( "Caixa 1" ));
getContentPane (). add ( new JTextField ());
getContentPane (). add ( new JLabel ( "Caixa 2" ));
getContentPane (). add ( new JTextField ());
getContentPane (). add ( new JLabel ( "Caixa 3" ));
getContentPane (). add ( new JTextField ());
getContentPane (). add ( new JButton ( "Opcao 1" ));
getContentPane (). add ( new JButton ( "Opcao 2" ));
getContentPane (). add ( new JButton ( "Opcao 3" ));
getContentPane (). add ( new JButton ( "Opcao 4" ));
getContentPane (). add ( new JButton ( "Opcao 5" ));
getContentPane (). add ( new JButton ( "Opcao 6" ));
}
}