Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Last active July 13, 2017 17:15
Show Gist options
  • Save Blasanka/6fc55a57603a25c5ab136a470e2303ce to your computer and use it in GitHub Desktop.
Save Blasanka/6fc55a57603a25c5ab136a470e2303ce to your computer and use it in GitHub Desktop.
This java code to show you how to close one JFrame from a button click without closing another JFrame(dispose() in Window class) and how to close all the JFrame's using a button. Note: this code only open up a new JFrame and code for that frame is here: https://gist.github.com/Blasanka/e1a19df553d1e502b4c3aacaa3c6f7c0 In that link you can see al…
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainFrame {
JFrame mainFrame;
JLabel label;
JButton openBt;
public MainFrame(){
initComponent();
}
public void initComponent(){
mainFrame = new JFrame("Frame 1");
mainFrame.setSize(300, 300);
mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(),BoxLayout.PAGE_AXIS));
label = new JLabel("Frame 1");
mainFrame.add(label);
openBt = new JButton("Click");
openBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SecondFrame().secondFrame.setVisible(true);
}
});
mainFrame.add(openBt);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment