繁体   English   中英

如何从jmenubar弹出jframe

[英]how to popup jframe from jmenubar

我使用jmenubar,选项之一是关于。 单击“关于”后,我想弹出新窗口并显示一些信息。 怎么做? 感谢您的帮助:)我附上我的代码。

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);
    }

}

看看如何制作对话框

一般建议是使用JOptionPane因为它提供了一个现成的对话框,您可以在其中显示内容,例如...

关于

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

对于更复杂的输出,您可以使用<html>文本,甚至可以使用某种容器/组件(例如JPanel ,并以所需的方式布置其他组件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM