gpt4 book ai didi

java - JFrame 之前的 JDialog

转载 作者:行者123 更新时间:2023-12-01 19:02:49 27 4
gpt4 key购买 nike

我正在使用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; } 函数到 SetupDialog 类。
  2. SetupDialog类的InputField内部设置此变量。
  3. WheelOfFortuneFrame 构造函数中,在 SetDialog.setVisible(true) 返回后执行 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com