1

Quiero cerrar el jFrame campeones desde el boton de otro Jframe, intente el dispose y el setVisible y nada no se como se haria, este sería el codigo del boton epro el dispose no funciona como ya dije.

 private void jButton_DetallesActionPerformed(java.awt.event.ActionEvent evt) {                                                 
       
        Campeones.dispose();
        new DatosCampeon(Nombrepj).setVisible(true);
        
        
    } 

Alguna idea?

1 respuesta 1

1

setVisible funciona bien, pero necesita que un JFrame pertenezca a otro, el JFrame que desea cerrar debe ser un elemento secundario del JFrame con los botones, o al menos tener un método estático para controlar su visibilidad.

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
    
    private Ventana2 v2 = new Ventana2(this);
    
    JButton abrir = new JButton("abrir");
    JButton cerrar = new JButton("cerrar");
    
    public Main() {
        crearVentana();
        crearbotones();
    }

    private void crearbotones() {
        abrir.setBounds(10, 20, 150, 30);
        cerrar.setBounds(10, 60, 150, 30);
        
        abrir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                v2.setVisible(true);
            }
        });
        
        cerrar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                v2.setVisible(false);
            }
        });
        
        add(abrir);
        add(cerrar);
    }

    private void crearVentana() {
        setSize(new Dimension(800, 800));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(null);
    }
    
    public static void main(String[] args) {
        Main m = new Main();
    }
    

}

class Ventana2 extends JFrame {
    public Ventana2(JFrame padre) {
        setVisible(false);
        setSize(new Dimension(500, 500));
        setLocationRelativeTo(padre);
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.