Swing - remove the frame

My goal is an action listener to close a specific JFrame when the user hits JButton to exit.

In general, when a program launches a large JFrame, a small one opens in front .... in my code, the user enters some details in this small one and falls into submit (for simplicity, ive skipped this code here and replaced submit with quit)

So, when these close buttons are pressed. I expect this little JFrame to close. I can not understand this. Listeners of actions in another class and ive tried to make examples and were out of luck. I commented on the code that I tried to use when trying to solve this problem.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class test
{
    public static void main(String Args[])
    {
    makeGUI m = new makeGUI();
    }
}

class makeGUI
{
    JButton close = new JButton("CLOSE ME");

    makeGUI()
    {
    frame f1 = new frame();

    JFrame smallframe = new JFrame(); //want to close this one
    JPanel jp = new JPanel(new FlowLayout());
    smallframe.setSize(300,300);
    smallframe.setLocationRelativeTo(null);
    smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
    close.addActionListener(new action());
    jp.add(close);
    smallframe.add(jp);
    smallframe.setVisible(true);
    }

    class action implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        //makeGUI s1 = new makeGUI();
        if (e.getSource () == close)
        {
            //s1.smallframe.dispose();
            System.out.println("gotcha");
        }
    }
    }    
}

class frame extends JFrame
{
    frame ()
    {
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("big one");
    setVisible(true);
    }
}
+4
source share
2 answers

-, , , - MakeGUI MakeGUI.

, MakeGUI . , , , . , , . , , :

class MakeGUI {
   JFrame smallframe;
   JButton close = new JButton("CLOSE ME");


   MakeGUI() {
       frame f1 = new frame();
       smallframe = new JFrame(); //want to close this one
       JPanel jp = new JPanel(new FlowLayout());
       smallframe.setSize(300, 300);
       smallframe.setLocationRelativeTo(null);
       smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
       close.addActionListener(new action());
       jp.add(close);
       smallframe.add(jp);
       smallframe.setVisible(true);
   }

   class action implements ActionListener {
       public void actionPerformed(ActionEvent e) {
           if (e.getSource() == close) {
               // use this instead of dispose
               smallframe.dispatchEvent(new WindowEvent(smallframe, WindowEvent.WINDOW_CLOSING));
               System.out.println("gotcha");
           }
       }
   }
}
+4

, - [X], :

smallFrame.dispatchEvent(new WindowEvent(smallFrame, WindowEvent.WINDOW_CLOSING));

, , , . smallFrame.

, JFrame ActionListener smallFrame .

, JFrame . , JOptionPane, " ".

:

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

+3

All Articles