如何从菜单栏打开新窗口?

时间:2012-04-25 00:02:56

标签: java swing menu jframe joptionpane

到目前为止,这是我的代码:

public static void main(String[] args){
    JFrame frame = new JFrame("Breakout!");
    frame.setLocation(300, 300);
    //Making the menubar
        JMenuBar mb = new JMenuBar();
        frame.setJMenuBar(mb);
        JMenu fileMenu = new JMenu("File");
        mb.add(fileMenu);
        JMenuItem newAction =   new JMenuItem("About");
        fileMenu.add(newAction);
        newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("open new window here");
            }
        });
    BreakoutCourt c = new BreakoutCourt();
    frame.add(c);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

我正在制作突围游戏。我想创建一个关于窗口,显示有关游戏的信息(比如,如何播放等等)。我该怎么做呢?感谢您的帮助!我是Java Swing的新手,所以是的。

1 个答案:

答案 0 :(得分:5)

您可以使用消息类型JOptionPane执行简单的操作:

JOptionPane.showMessageDialog(frame, "Breakout! v1.0");

(你需要让帧最终完成,因为它是从匿名动作监听器中访问的。)

为了更好地控制显示的内容,以及它是否应该是模态的,您可以查看JDialog。这里概述了在Swing中使用对话框:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

相关问题