如何在菜单栏中打开新窗口?

4
这是我的代码,至此为止:
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个回答

5

您可以使用JOptionPane消息类型创建一个简单的对话框:

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

(你需要将框架设为final,以便从匿名动作侦听器中访问它们)。

如果想更多地控制显示内容和模态的设置,可以看一下 JDialog。关于在Swing中使用对话框的概述可以参考这里:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html


1
另请参阅使用多个JFrames,好的/坏的做法? - Andrew Thompson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接