• Sábado 21 de Septiembre de 2024, 14:23

Mostrar Mensajes

Esta sección te permite ver todos los posts escritos por este usuario. Ten en cuenta que sólo puedes ver los posts escritos en zonas a las que tienes acceso en este momento.


Temas - darkness_twilight

Páginas: [1]
1
Java / Problema En Manejo De Varios Jframe
« en: Jueves 19 de Octubre de 2006, 03:32 »
Hola

Tengo un problema con una agenda k estoy empezando a programar, lo que pasa es k este programa empieza mostrando un menu principal con opciones( botones ) como ingresar datos, salir, etc. hasta el momento solo le tengo dos opciones, ingresar datos y salir. Cuando se presiona salir, al JFrame principal le aplico el metodo setvisible( false ); para ocultarlo, e instanciar un nuevo JFrame para el ingreso de datos, todos los JFrame son clases distintas k extienden JFrame obviamente. El problema que tengo es que estando en el JFrame de ingreso de datos, y presionando el boton volver ( lo que cierra el JFrame de ingreso y hace volver al JFrame principal ) no se como hacer para manejar la instancia de la clase JFrame principal para aplicar el metodo setVisible( true ); para volver a mostrarla, podria hacer una nueva instancia de la clase, pero esa no es la idea, la idea es hacer reaparecer el JFrame k esta invisible, espero k puedan ayudarme.

Aqui va el codigo, son tres clases distintas ( en archivos distintos) una en cada archivo:

Código: Text
  1.  
  2.  
  3. // Archivo LibretaContactos.java
  4.  
  5. import javax.swing.*;
  6.  
  7. public class LibretaContactos
  8. {
  9.   public static void main( String args[] )
  10.   {
  11.     MenuPrincipal frame = new MenuPrincipal();
  12.    
  13.     frame.setResizable( false );
  14.     frame.setVisible( true );
  15.   }
  16. }
  17.  
  18. // Archivo MenuPrincipal.java
  19.  
  20. import javax.swing.*;
  21. import java.awt.*;
  22. import java.awt.event.*;
  23.  
  24. public class MenuPrincipal extends JFrame
  25. {
  26.   private JButton btnIngresar;
  27.   private JButton btnSalir;
  28.   private JLabel lblTitulo;
  29.   private JLabel lblEstadoServidor;
  30.   private JPanel panelTitulo;
  31.   private JPanel panelCentral;
  32.   private JPanel panelInferior;
  33.  
  34.   public MenuPrincipal()
  35.   {
  36.     super( " CONTACTOS :: Darkness_Twilight :: " );
  37.  
  38.     inicializarComponentes();
  39.   }
  40.  
  41.   private void inicializarComponentes()
  42.   {
  43.     setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  44.     setSize( 240, 350 );
  45.     setLayout( new BorderLayout( 15, 5 ) );
  46.     setLocationRelativeTo( null );
  47.     setUndecorated( false );
  48.  
  49.     // JPanel's
  50.     panelTitulo = new JPanel();
  51.     panelTitulo.setBackground( Color.BLUE );
  52.     panelCentral = new JPanel();
  53.     panelCentral.setBackground( Color.BLUE );
  54.     panelInferior = new JPanel();
  55.     panelInferior.setBackground( Color.WHITE );
  56.  
  57.     ManejadorBoton handler = new ManejadorBoton();
  58.  
  59.     // JButton's
  60.     btnIngresar = new JButton( "Ingresa tus datos!!..." );
  61.     btnIngresar.setFont( new Font( "Arial", Font.BOLD, 14 ) );
  62.     btnIngresar.setForeground( Color.BLACK );
  63.     btnIngresar.addActionListener( handler );
  64.  
  65.     btnSalir = new JButton( "Salir" );
  66.     btnSalir.setFont( new Font( "Arial", Font.BOLD, 14 ) );
  67.     btnSalir.setForeground( Color.BLACK );
  68.     btnSalir.addActionListener( handler  );
  69.  
  70.     // JLabel's
  71.     lblTitulo = new JLabel( "Menu Principal" );
  72.     lblTitulo.setFont( new Font( "Century Gothic", Font.BOLD, 26 ) );
  73.     lblTitulo.setForeground( Color.BLACK );
  74.  
  75.     lblEstadoServidor = new JLabel( "Servidor Desconectado..." );
  76.     lblEstadoServidor.setFont( new Font( "SansSerif", Font.BOLD, 16 ) );
  77.     lblEstadoServidor.setForeground( Color.RED );
  78.  
  79.     agregarComponentes();
  80.  
  81.   }
  82.  
  83.   private class ManejadorBoton implements ActionListener
  84.   {
  85.     public void actionPerformed( ActionEvent evento )
  86.     {
  87.       if( evento.getSource() == btnSalir )
  88.         System.exit( 0 );
  89.  
  90.       if ( evento.getSource() == btnIngresar )
  91.       {
  92.                               setVisible( false );
  93.                               JFrameIngreso frameIngreso = new JFrameIngreso();
  94.                               frameIngreso.setVisible( true );
  95.                               frameIngreso.toFront();
  96.                         }
  97.     }
  98.   }
  99.  
  100.   private void agregarComponentes()
  101.   {
  102.     panelTitulo.add( lblTitulo );
  103.     panelCentral.add( btnIngresar );
  104.     panelCentral.add( btnSalir );
  105.     panelInferior.add( lblEstadoServidor );
  106.  
  107.     add( panelTitulo, BorderLayout.NORTH );
  108.     add( panelCentral, BorderLayout.CENTER );
  109.     add( panelInferior, BorderLayout.SOUTH );
  110.  
  111.   }
  112. }
  113.  
  114. // Archivo JFrameIngreso.java
  115.  
  116. import javax.swing.*;
  117. import java.awt.*;
  118. import java.awt.event.*;
  119.  
  120. public class JFrameIngreso extends JFrame
  121. {
  122.   private JLabel lblTitulo;
  123.   private JPanel panelCampos, panelBotones, panelNombre, panelApellido,                      
  124.                              panelCorreo;
  125.   private JLabel lblNombre, lblApellido, lblFechaNac, lblCorreo, lblFonoFijo,
  126.                             lblFonoMovil;
  127.   private JTextField txtNombre, txtApellido, txtFechaNac, txtCorreo, txtFonoFijo,
  128.                                   txtFonoMovil;
  129.         private JButton btnIngresar, btnVolver;
  130.  
  131.   public JFrameIngreso()
  132.   {
  133.     inicializarComponentes();
  134.   }
  135.  
  136.   private void inicializarComponentes()
  137.   {
  138.     inicializarJFrame();
  139.     inicializarJPanel();
  140.     inicializarJLabel();
  141.     inicializarJTextField();
  142.     inicializarJButton();
  143.  
  144.     agregarComponentes();
  145.   }
  146.  
  147.   private void inicializarJFrame()
  148.   {
  149.               setVisible( false );
  150.     setSize( 250, 250 );
  151.     setTitle( "Informacion Contacto" );
  152.     setLayout( new BorderLayout( 0, 10 ) );
  153.     setLocationRelativeTo( null );
  154.     setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  155.  
  156.   }
  157.  
  158.   private void inicializarJLabel()
  159.   {
  160.     lblTitulo = new JLabel( "Ingreso de Datos" );
  161.     lblTitulo.setFont( new Font( "Chlorinar", Font.BOLD, 24 ) );
  162.     lblTitulo.setHorizontalAlignment( SwingConstants.CENTER );
  163.  
  164.     lblNombre = new JLabel( "Nombre: " );
  165.     lblNombre.setFont( new Font( "Arial", Font.PLAIN, 12 ) );
  166.  
  167.     lblApellido = new JLabel( "Apellido: " );
  168.     lblApellido.setFont( new Font( "Arial", Font.PLAIN, 12 ) );
  169.  
  170.     lblCorreo = new JLabel( "Correo: " );
  171.     lblCorreo.setFont( new Font( "Arial", Font.PLAIN, 12 ) );
  172.   }
  173.  
  174.   private void inicializarJPanel()
  175.   {
  176.     panelCampos = new JPanel( new GridLayout( 3, 1 ) );
  177.  
  178.     panelBotones = new JPanel( new FlowLayout( ) );
  179.  
  180.     panelNombre = new JPanel( new FlowLayout( SwingConstants.LEFT ) );
  181.  
  182.     panelApellido = new JPanel( new FlowLayout( SwingConstants.LEFT ) );
  183.  
  184.     panelCorreo = new JPanel( new FlowLayout( SwingConstants.LEFT ) );
  185.   }
  186.  
  187.   private void inicializarJTextField()
  188.   {
  189.     txtNombre = new JTextField();
  190.     txtNombre.setColumns( 15 );
  191.  
  192.     txtApellido = new JTextField();
  193.     txtApellido.setColumns( 15 );
  194.  
  195.     txtCorreo = new JTextField();
  196.     txtCorreo.setColumns( 15 );
  197.   }
  198.  
  199.   private void inicializarJButton()
  200.   {
  201.               ManejadorBoton handler = new ManejadorBoton();
  202.  
  203.               btnIngresar = new JButton( "Ingresar" );
  204.               btnIngresar.setFont( new Font( "Monospaced", Font.BOLD, 14 ) );
  205.                 btnIngresar.setBounds( new Rectangle( 30, 40 ));
  206.  
  207.              btnVolver = new JButton( "Volver" );
  208.               btnVolver.setFont( new Font( "Monospaced", Font.BOLD, 14 ) );
  209.               btnVolver.addActionListener( handler );
  210.   }
  211.  
  212.   private void agregarComponentes()
  213.   {
  214.              panelNombre.add( lblNombre );
  215.              panelNombre.add( txtNombre );
  216.              panelApellido.add( lblApellido );
  217.              panelApellido.add( txtApellido );
  218.              panelCorreo.add( lblCorreo );
  219.              panelCorreo.add( txtCorreo );
  220.  
  221.     panelCampos.add( panelNombre );
  222.     panelCampos.add( panelApellido );
  223.     panelCampos.add( panelCorreo );
  224.  
  225.     panelBotones.add( btnIngresar );
  226.     panelBotones.add( btnVolver );
  227.  
  228.     add( lblTitulo, BorderLayout.NORTH );
  229.     add( panelCampos, BorderLayout.WEST );
  230.     add( panelBotones, BorderLayout.SOUTH );
  231.   }
  232.  
  233.   private class ManejadorBoton implements ActionListener
  234.   {
  235.     public void actionPerformed( ActionEvent evento )
  236.     {
  237.       if ( evento.getSource() == btnVolver )
  238.       {
  239.                                setVisible( false );
  240.                               //Aqui quiero volver a mostrar el JFrame MenuPrincipal
  241.                              //con setVisible( true ), pero no se como me puedo referir a el
  242.                         }
  243.     }
  244.   }
  245. }
  246.  
  247.  

Espero su respuesta, o una idea o forma alternativa de hacerlo, la idea es k cada JFrame sea una clase distinta, quiero k sea lo mas modular posible, vale.

Páginas: [1]