【问题标题】:How do I open new windows from menubars?如何从菜单栏打开新窗口?
【发布时间】:2012-05-05 15:57:39
【问题描述】:

这是我目前的代码:

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 很陌生,所以是的。

【问题讨论】:

    标签: java swing menu jframe joptionpane


    【解决方案1】:

    你可以用JOptionPane的消息类型做一个简单的:

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

    (您需要将帧设置为最终帧才能执行此操作,因为它是从匿名操作侦听器中访问的)。

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

    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多