我正在使用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();
    }
}


但是我发现在构造Wheelwheel之前无法更改WheelOfFortuneFrame的成员变量,这意味着我无法使用用户输入的值来构造主窗口。

最佳答案

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


numberOfPlayers变量和getNumberOfPlayers() { return numberOfPlayers; }函数添加到SetupDialog类。
InputField类内的SetupDialog设置此变量。
WheelOfFortuneFrame构造函数中,在SetDialog.setVisible(true)之后返回do numberOfPlayers = SetDialog.getNumberOfPlayers();


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

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);
   }
}

关于java - JFrame之前的JDialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59606690/

10-10 09:54