2012-04-25 32 views
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很陌生,所以是的。

回答

5

你可以做一個簡單的與JOptionPane消息類型:

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

(你需要做的框架最終要做到這一點,因爲它正在從匿名動作監聽器中訪問)。

在過去顯示的內容更多的控制權,以及是否應該是有模式或沒有,你可以看看JDialog。這裏有一個在Swing中使用對話框的概述:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

+0

非常有幫助!非常感謝!! – flymonkey 2012-04-25 01:14:11

+1

+1另請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) – 2012-04-25 08:12:01