I have a menu bar in a JFrame JF when i click on the menubar item a new JFrame JF1 is created and is displayed , but when i clicked on the close button of JF1 , both JFrame are closed . When i click on close button of JF1 , i want only JF1 to be closed , not JF
JMenuBar menubar;
JMenu help;
JMenuItem about;
public GUI() {
setLayout(new FlowLayout());
menubar = new JMenuBar();
add(menubar);
help = new JMenu("Help");
menubar.add(help);
}`
a new JFrame JF1 is created and is displayed
Don't create a new JFrame an application should only have a single JFrame.
Instead create a JDialog
. See: The Use of Multiple JFrames: Good or Bad Practice? for more information.
Also you don't add a JMenuBar to a JFrame using the add(...) method. See How to Use Menu Bars for the better way to do that.
I recomend you to use DesktopPane and JInternalFrame.
To the main Frame you make a change: contentPane (JPanel) will be JDesktopPane.
The JFrame that it's displayed whit the click will be a JInternalFrame.
In the actionListener of the JMenuItem, you will do this:
MyInternalFrame internalFrame = new MyInternalFrame();
internalFrame.show();
contentPane.add(internalFrame);
MyInternalFrame is the class of the Frame displayed (a class that extends JInternalFrame).
To close "internalFrame", just add a button to the layout whit the text "Quit" and in the actionListener of its you put "dispose()".
Try it and tell if it works ;)
--EDIT--- MAIN CLASS (The JFRAME)
public class Main extends JFrame {
private JDesktopPane contentPane;
private JMenuBar menuBar;
private JMenu mnExample;
private JMenuItem mntmAbout;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(20, 20, 800, 800);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnExample = new JMenu("Help");
menuBar.add(mnExample);
mntmAbout = new JMenuItem("About");
mntmAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
About frame = new About();
frame.show();
contentPane.add(frame);
}
});
mnExample.add(mntmAbout);
contentPane = new JDesktopPane();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
}
ABOUT CLASS (The JINTERNALFRAME)
public class About extends JInternalFrame {
public About() {
setBounds(100, 100, 544, 372);
JLabel lblSomeText = new JLabel("Some Text");
getContentPane().add(lblSomeText, BorderLayout.CENTER);
JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
getContentPane().add(btnClose, BorderLayout.SOUTH);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.