JFrame.HIDE_ON_CLOSE

HIDE_ON_CLOSE : The method is used to hide the frame, but the frame doesn’t not terminate.

Program

1.0 fghgfhg

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Frame extends JFrame implements ActionListener{
   
    private JButton jb1,jb2,jb3;
    private JFrame sub;
    
    Frame()
    {
        setTitle("Main Frame");
        setLayout(new FlowLayout());
        setButton();
        setAction();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setButton()
    {
        jb1 = new JButton("Create Frame");
        add(jb1);
        jb2 = new JButton("Show Frame");
        add(jb2);
    }
    
    private void setAction()
    {
        jb1.addActionListener(this);
        jb2.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent eve)
    {
        if(eve.getSource() == jb1)
        {
            if(sub == null)
            {
                makeSubFrame();
            }
            else
                JOptionPane.showMessageDialog(rootPane,"Frame Already Created");
        }
        else if(eve.getSource() == jb2)
            sub.setVisible(true);
    }
    
    void makeSubFrame()
    {
        sub = new JFrame();
        sub.setTitle("Sub Frame");
        sub.setLayout(new FlowLayout());
        jb3 = new JButton("UnChanged");
        jb3.addActionListener(new ActionListener(){
                                  public void actionPerformed(ActionEvent eve)
                                  {
                                   jb3.setText("Changed");
                                  }
                                }
                             );
        sub.add(jb3);
        sub.setSize(400, 200);
        sub.setVisible(true);
        sub.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
        
       Frame fr = new Frame();
    }
}

Leave a Comment