Search code examples
javaswingjframejmenujmenubar

how to popup jframe from jmenubar


I use jmenubar, and one of the option is about. I want to popup new window after click "about" and show some information. How to do that? Thanks for you help :) I enclose my code.

public class Frame extends JFrame{
public Frame(){
    JFrame frame = new JFrame("Program");
    final JFrame window = new JFrame("About");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700,700);
    window.setSize(200,200);
    frame.setLocation(50,50);
    window.setLocation(150,150);
    frame.setResizable(false);
    window.pack();
    frame.setVisible(true);


    JMenuBar menu = new JMenuBar();
    frame.setJMenuBar(menu);

    JMenu file = new JMenu("File");
    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_C);
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    file.add(exit);

    JMenu about = new JMenu("About");
    //oprogramie.setMnemonic(KeyEvent.VK_C);
    about.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //JOptionPane.showMessageDialog(null, "Your message goes here!","Message", JOptionPane.ERROR_MESSAGE);
            window.setVisible(true);
        }
    });


    menu.add(file);
    menu.add(about);
    }

}


Solution

  • Take a look at How to Make Dialogs

    General advice would be to use a JOptionPane as it provides a ready made dialog into which you can display stuff, for example...

    About

    JOptionPane.showMessageDialog(null, "Hello world", "About", JOptionPane.INFORMATION_MESSAGE);
    

    For more complex output, you can use <html> text or even a container/component of some kind, like a JPanel, with other components laid out the way you want them