minte9
LearnRemember





Frame

Java Swing is used to create window-based applications. A JFrame is the object that represents a window on the screen.
 
import javax.swing.*;

public class App {

    public static void main(String[] args) {
     
        JFrame frame = new JFrame(); // make a frame
        frame.setSize(300, 200);
        frame.setVisible(true);
        
        // when window closed, stop the program
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // make a button
        JButton button = new JButton("click me"); 

        // add button to frame
        frame.getContentPane().add(button);
    }
}
A dialog window is an independent window for temporary notice.
 
import java.awt.*;
import javax.swing.*;

public class App extends JFrame {

    public static void main(String[] args) {
    
        App frame = new App();
        
        frame.setBounds(200, 200, 400, 300);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
        
        JOptionPane.showMessageDialog(null, "Bad format!");
            // Message in the middle of the screen
        
        JOptionPane.showMessageDialog(frame, "Bad format!");  // Look Here
            // Message in the middle of the frame  
    }
}

New Frame

Use setVisible() method to open a new frame.
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class App extends JFrame {
    
    static JFrame frame2 = new JFrame();
    
    public static void main(String[] args) {
    
        App frame = new App();
    
        frame.setBounds(200, 200, 400, 300);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);

        frame2.setBounds(350, 350, 300, 200);
        frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame2.setVisible(false);
    }
    
    public App() {
    
        JPanel panel = new JPanel();
        
        JButton button = new JButton("Open");
            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    
                    frame2.setVisible(true); // Look Here
                }
            });
            
            panel.add(button);
            
            add(panel);
        }
}
Use getTopLevelAncestor() to get parent frame.
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class App extends JFrame {
    
    static Frame2 frame2 = new Frame2();
    
    public static void main(String[] args) {
    
        App frame = new App() {{
            setBounds(200, 200, 300, 200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }};
    }
    
    public App() {
    
        JPanel panel = new JPanel() {{
            JButton button = new JButton("Open") {{
                addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        frame2.setVisible(true);
                    }
                });
            }};
            add(button);
        }};
        add(panel);
    }
    
    public static class Frame2 extends JFrame {
        
        public Frame2() {
            
            setBounds(250, 250, 200, 100);
            setVisible(false);

            JPanel panel = new JPanel() {{
               JButton button = new JButton ("Cancel") {{
                   addActionListener(new ActionListener() {
                       @Override public void actionPerformed(ActionEvent e) {

                           getTopLevelAncestor().setVisible(false); // corect - Look Here
                               // setVisible(false); // this only hides the button
                       }
                   });
               }}; 
               add(button);
            }};
            add(panel);
        }
    }
}



  Last update: 382 days ago