首页 > 解决方案 > JFrame 之前的 JDialog

问题描述

我正在使用 java 实现游戏“命运之轮”。在显示主窗口之前,我需要先创建一个对话框并从用户那里读取玩家信息,以便我可以使用这些信息来设置我的主窗口。

主要功能代码如下(受讲师限制):

public static void main(String[] args) { 
    WheelOfFortuneFrame gameFrame = new WheelOfFortuneFrame();
    gameFrame.pack();
    gameFrame.setVisible(true);
 }

所以只能在 WheelOfFortuneFrame 的构造函数中添加代码。我要做的是在 Wheelof ForturneFrame 的构造函数中创建一个 JDialog,如下所示:

public class WheelOfFortuneFrame extends JFrame(){

 // Some member variables
 private numberofPlayers = 0;

 public WheelOfFortuneFrame() {
    super("Wheel of Fortune");

    SetDialog = new SetupDialog(null);
    SetDialog.setVisible(true);

    // Things for the main window
 }
}

我尝试在对话框中更改 WheelOfFortuneFrame 类的成员变量,例如:

public final class SetupDialog extends JDialog{

    public SetupDialog(JFrame mainframe){
       .......
       numberofVariables = InputField.getText();
    }
}

但是我发现在构建WheelOfFortuneFrame之前我没有改变它的成员变量,这意味着我不能使用用户输入的值来构建我的主窗口。

标签: java

解决方案


您不能从类中访问WheelOfFortuneFrame变量。SetupDialog但是,您可以执行以下操作:

  1. 向类中添加一个numberOfPlayers变量和一个getNumberOfPlayers() { return numberOfPlayers; }函数SetupDialog
  2. 从类InputField 内部设置此变量SetupDialog
  3. WheelOfFortuneFrame构造函数中,SetDialog.setVisible(true)返回后做numberOfPlayers = SetDialog.getNumberOfPlayers();

这是一个简短的示例,它在显示框架之前显示一个对话框。请注意,使用 aJOptionPane而不是手工制作的对话框可能更方便。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public final class DialogBeforeFrame extends JFrame {
   /** Dialog that prompts for a single string as input. */
   private static class SetupDialog extends JDialog {
      final JTextField input;
      public SetupDialog() {
         super();
         setModal(true);
         setLayout(new BorderLayout());
         input = new JTextField("Some text");
         add(input, BorderLayout.CENTER);
         add(new JButton(new AbstractAction("Ok") {
               public void actionPerformed(ActionEvent e) {
                  setVisible(false);
               }
            }), BorderLayout.SOUTH);
      }
      public String getInput() { return input.getText().trim(); }
   }

   /** Constructor that takes the text to display as argument. */
   public DialogBeforeFrame(String text) {
      super();
      add(new JLabel(text));
   }
   /** Constructor that shows a dialog which prompts for the text to be displayed. */
   public DialogBeforeFrame() {
      super();

      final SetupDialog dialog = new SetupDialog();
      dialog.pack();
      dialog.setVisible(true);

      add(new JLabel(dialog.getInput()));
   }


   public static void main(String[] args) {
      JFrame frame = null;

      // Two variants of constructing the frame:
      // 1. We first show the dialog, extract the configuration from the dialog
      //    and then construct the frame with these arguments.
      // 2. We have the frame's constructor show a dialog for configuration.
      if ( false ) {
         final SetupDialog dialog = new SetupDialog();
         dialog.pack();
         dialog.setVisible(true);

         frame = new DialogBeforeFrame(dialog.getInput());
      }
      else {
         frame = new DialogBeforeFrame();
      }

      frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
         });
      frame.pack();
      frame.setVisible(true);
   }
}

推荐阅读