How to Dispose an opened Jframe from Other

2019-08-30 22:22发布

问题:

Hi i was wondering how to Dispose a jFrame from another one, cause i want to create a new instance of that class with new values in its textfields, so the First jFrame is this:

public class Frame1 extends javax.swing.JFrame implements ActionListener {
    Frame2 f;
    public Frame1() {
        initComponents();
        this.setLocationRelativeTo(null);
    }
    private void rbtnShowFrame2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        f = new Frame2();
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        this.dispose(); //I TRIED TO DISPOSING IT HERE BUT DOESN'T WORK
    }
}

So i want in the other jFrame Dispose the jFrame1 only if i trigger the event action performed of a botton, if this doesn't happen i do't want to dispose it, i don't know if i can do it with ActionListener, this is the Second jFrame:

public class Frame2 extends javax.swing.JFrame {
    public Frame2() {
        initComponents();
        this.setLocationRelativeTo(null);
        Frame1 f1 = new Frame1();
        this.cmdOk.addActionListener(cGUI);
    }
    private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //Here is where i want to dispose() the other jFrame1
        //to create a new instance and pass the value using public static jTextFields
        f1.labelNumeroCotizacion.setText(this.labelNumCotizacionEnviar.getText());
        f1.setVisible(true);
    }
} 

Sorry for my Code, i am newbie using OOP! thanks for all guys....

回答1:

Here is an example of how to dispose a JFrame from another JFrame:

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

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

public class Demo
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        FrameA one = new FrameA();
        FrameB two = new FrameB(one);

        one.setVisible(true);
        two.setVisible(true);
      }
    });
  }
}

class FrameA extends JFrame
{
  private static final long serialVersionUID = 1812279930292019387L;

  public FrameA()
  {
    super("Frame A");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    setLocationRelativeTo(null);

    setResizable(false);
  }
}

class FrameB extends JFrame
{
  private static final long serialVersionUID = 5126089271972476434L;

  public FrameB(final JFrame otherFrame)
  {
    super("Frame B");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    setLayout(new GridBagLayout());
    setLocationRelativeTo(otherFrame);

    JButton button = new JButton("Dispose Other");
    button.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        otherFrame.dispose();
      }
    });

    add(button);

    setResizable(false);
  }
}