I am implementing the game "Wheel of Fortune" using java. Before I display the main window, I need to first create a dialog and read in player informations from users so that I can use these information to set up my main window.
The main function code looks like (restricted by instructor):
public static void main(String[] args) {
WheelOfFortuneFrame gameFrame = new WheelOfFortuneFrame();
gameFrame.pack();
gameFrame.setVisible(true);
}
So I can only add code in the constructor of WheelOfFortuneFrame. What I am trying to do is creating a JDialog in the constructor of Wheelof ForturneFrame, like this:
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
}
}
I tried to change the member variables of WheelOfFortuneFrame class in the Dialog, like:
public final class SetupDialog extends JDialog{
public SetupDialog(JFrame mainframe){
.......
numberofVariables = InputField.getText();
}
}
But I found that I failed to change member variables of WheelOfFortuneFrame before the constructing of it, which means I cannot use user-input value to construct my main window.
You cannot access the WheelOfFortuneFrame
variables from the SetupDialog
class. However, you can do the following:
numberOfPlayers
variable and a getNumberOfPlayers() { return numberOfPlayers; }
getNumberOfPlayers() { return numberOfPlayers; }
function to the SetupDialog
class. InputField
inside the SetupDialog
class.WheelOfFortuneFrame
constructor, after SetDialog.setVisible(true)
returns do numberOfPlayers = SetDialog.getNumberOfPlayers();
Here is a short example that shows a dialog before a frame is displayed. Note that using a JOptionPane
instead of a hand-crafted dialog may be more convenient.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.