繁体   English   中英

JFrame 之前的 JDialog

[英]JDialog before a JFrame

我正在使用 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 之前我没有改变它的成员变量,这意味着我不能使用用户输入的值来构造我的主窗口。

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

  1. 添加一个numberOfPlayers变量和一个getNumberOfPlayers() { return numberOfPlayers; } getNumberOfPlayers() { return numberOfPlayers; }功能的SetupDialog类。
  2. 设置从该变量InputField里面SetupDialog类。
  3. 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);
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM