我用Java做了一个小数字猜谜游戏。我的主 JFrame(主菜单)有三个 JButton,播放、声音和退出。
按下播放按钮开始我的游戏,一系列 JOptionPanes 出现要求用户输入数字。它工作正常,游戏运行正常。但是当我按播放按钮玩游戏时,我无法按退出或声音按钮或游戏中的任何其他按钮。我什至不能按下主 JFrame 窗口的 X(关闭)按钮,直到我完全玩游戏,或者关闭 JOptionPane 从而关闭当前游戏。
当我已经按下声音按钮开始背景声音时,我可以按下退出按钮。当我已经按下声音按钮时,我可以按下播放按钮。
有什么建议么?
我的问题是,假设我正在使用 JOptionPane 制作一个小游戏,当 JOptionPane 已经打开时,如何按下我的主 JFrame(主菜单)上存在的 JButton
这是我的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);
}
});
}