2012-10-14 44 views
0

我已经做了一个Java的小数猜测游戏。我的主要JFrame(主菜单)有三个JButtons,Play,Sound和Exit。如何从JOptionPane返回主菜单(JFrame)

按下播放按钮开始我的游戏,一系列JOptionPanes出现要求用户输入数字。它工作正常,游戏运行正常。但是当我按下播放按钮来玩游戏时,我无法按下游戏中的退出按钮或声音按钮或任何其他按钮。我甚至无法按下主JFrame窗口的X(关闭)按钮,直到我完全玩完游戏,或关闭JOptionPane以关闭当前游戏。

我可以按退出按钮,当我已经按下声音按钮启动背景声音。当我已经按下声音按钮时,我可以按播放按钮。

有什么建议吗?

我的问题是,假设我正在使用JOptionPanes如何按即存在于我的主要的JFrame(主菜单)Jbutton将在 一个的JOptionPane已经打开

这里的一个小游戏是我SSCCE

import javax.swing.*; 
import java.awt.event.*; 

class Test2 { 
    static JFrame frame; 
    static JPanel jp; 

    static JButton b1; 
    static JButton b2; 
    static JButton b3; 

    public static void main(String[] args) { 
     final long startTime = System.currentTimeMillis(); 
     frame=new JFrame("Game "); 
     jp=new JPanel(); 
     b1=new JButton("Play"); 
     b1.addActionListener (new Action()); 
     b2=new JButton("Exit"); 
     b2.addActionListener (new Action1()); 
     b3=new JButton("Sound"); 
     b3.addActionListener (new Action2()); 

     jp.add(b1); 
     jp.add(b2); 
     jp.add(b3); 

     frame.add(jp); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300,400); 
     frame.setVisible(true); 
    } 

    static class Action implements ActionListener { // For (game) Play button 
     public void actionPerformed (ActionEvent e) { 
      Thread bb=new Thread(new Runnable(){ 
       public void run(){ 
        new Test2().start(); 
       }}); 
      bb.setPriority(1); 
      bb.start(); 
     } 
    } 

    static class Action1 implements ActionListener { // For Exit button 
     public void actionPerformed (ActionEvent e) { 

      Thread tt=new Thread(new Runnable(){ 
       public void run(){ 
        int response = JOptionPane.showConfirmDialog(
          null, 
          "Exit application?", 
          "Confirm", 
          JOptionPane.YES_NO_OPTION, 
          JOptionPane.QUESTION_MESSAGE); 
        if (response == JOptionPane.NO_OPTION) { 

        } 
        else if (response == JOptionPane.YES_OPTION) { 
         System.exit(0); 
        } 
       } 
      }); 
      tt.setPriority(10); 
      tt.start(); 
     } 
    } 

    static class Action2 implements ActionListener { //For Sound Button 
     public void actionPerformed (ActionEvent e) { 

      try { 
       /* Code to play sound */ 
      } 
      catch(Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

    void start() { // sample game 
     JOptionPane.showMessageDialog(null,"Step 1 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 2 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 3 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 4 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 5 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 6 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 7 ..click OK to continue"); 
     JOptionPane.showMessageDialog(null,"Step 8 ..click OK to continue"); 
    } 
} 

在我的新的代码只有start()方法已经改变

void start()       // sample game 
{ 

JOptionPane pane = new JOptionPane(); 
// Configure via set methods 
dialog = pane.createDialog(null,"exp 1"); 
// the line below is added to the example from the docs 

dialog.setSize(300, 200); 

dialog.setModal(false); // this says not to block background components 

JButton nextButton = new JButton("Go to Dialog2"); 


dialog.add(nextButton); 
nextButton.setBounds(25,25,20,20); 


dialog.show(); 

JOptionPane pane2 = new JOptionPane(); 
// Configure via set methods 
dialog2 = pane2.createDialog(null,"exp 2"); 
// the line below is added to the example from the docs 
dialog2.setModal(false); // this says not to block background components 
// dialog2.show(); 






nextButton.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent arg0) { 

dialog2.setVisible(true); 
dialog.setVisible(false); 

} 
}); 





} 
+0

* 1 )问一个*特定*问题。 2)为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 3)不要在'JOptionPane'中放置'游戏'。请参阅[本答案](http://stackoverflow.com/a/9554657/418556)了解将游戏**置入**框架的其他策略的提示。 –

+0

编辑我的问题 –

回答

4

不幸的是,JOptionPane的是一个模式对象,你不能LEAV直到你通过所有的对话。 从制作,所有对话框都是模态的。每个showXxxDialog方法都会阻止当前线程,直到用户的交互完成。

您可以通过创建非模态对话框来解决您的问题。 Example to create non-modal dialog

相反,的JDialog类似于的JFrame,你可以在它里面添加按钮,事件侦听器。 A Simple Example

您可以为自己创建一个定制的JDialog类:)

an example of customized JDialog

我编辑你的代码只是想: “有什么建议”

void start() { // sample game 
    MyDialog dialog7 = new MyDialog(null); 
    MyDialog dialog6 = new MyDialog(dialog7); 
    MyDialog dialog5 = new MyDialog(dialog6); 
    MyDialog dialog4 = new MyDialog(dialog5); 
    MyDialog dialog3 = new MyDialog(dialog4); 
    MyDialog dialog2 = new MyDialog(dialog3); 
    MyDialog dialog1 = new MyDialog(dialog2); 

    dialog1.setVisible(true); 
} 
+0

这是一个相关的[示例](http://stackoverflow.com/a/12451673/230513)。 – trashgod

+0

@ user1022209 ..这个工作,我可以返回到主菜单,但所有的对话框同时弹出,它不允许程序运行,因为我想它 –

+0

由于对话框是非模态的,他们将不阻止用于弹出对话框的线程。当用户通过前一个对话框时,您可以让下一个对话框弹出作为触发事件。 :) – code4j