java - 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之前我没有改变它的成员变量,这意味着我不能使用用户输入的值来构建我的主窗口。
解决方案
您不能从类中访问WheelOfFortuneFrame
变量。SetupDialog
但是,您可以执行以下操作:
- 向类中添加一个
numberOfPlayers
变量和一个getNumberOfPlayers() { return numberOfPlayers; }
函数SetupDialog
。 - 从类
InputField
内部设置此变量SetupDialog
。 - 在
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);
}
}
推荐阅读
- angular - CanActivate 中的 Angular Http 调用被取消
- java - 如何从 Java 应用程序启用 https
- c# - 主菜单的设置菜单未在暂停菜单的设置菜单中更新。统一 C#
- python - 我正在尝试打印包含带有换行符的字符串的字典,但打印的是“\n”而不是换行符
- vue.js - 为什么我的 Quasar Android 应用程序没有更新?
- php - 如何打开放置在iframe html中直接访问阻止目录中的pdf文件
- java - SpringMVC 无法将实体写入 json
- appium - 为什么 MoveToElement 没有响应
- http-headers - 如何从 Content-Disposition 修改 HTTP 响应标头:收到 API 响应后附加到内联
- azure - 以编程方式获取 ADF 管道消耗报告