Я настраиваю игру, используя JPanel, которая требует, чтобы пользователь вводил некоторую информацию, прежде чем играть. У меня есть методы в моем классе пользовательского интерфейса, который получает ввод от пользователя. Во-первых, я получаю количество палочек, с которыми хочет играть пользователь. Затем я получаю режим, в который пользователь хочет играть. Я вызываю ui.run (), а затем ui.getMode (), но они запускаются одновременно, потому что программа не ждет ui.run (), чтобы получить ввод пользователя.
Вот мой код:
public int run() {
createButton("Play", 150, 275, 200, 50, true);
welcomeLabel.setText("Welcome to the game of sticks!");
welcomeLabel.setBounds(162, 0, 200, 200);
createLabel("How many sticks are there on the table initially (10-100)? ", 100, 175, true);
label.setText("How many sticks are there on the table initially (10-100)? ");
label.setBounds(100, 175, 400, 50);
textField.setBounds(190, 225, 130, 30);
frame.add(textField);
frame.add(welcomeLabel);
frame.add(label);
frame.setTitle("Game of Sticks");
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!(textField.getText().isEmpty())) {
input = Integer.parseInt(textField.getText());
if(input >= 10 && input <= 100) {
welcomeLabel.setVisible(false);
label.setVisible(false);
textField.setVisible(false);
button.setVisible(false);
}
}
}
});
return input;
}
public int getMode() {
createButton("Submit", 150, 320, 200, 50, true);
optionLabel.setText("Options:");
optionLabel.setBounds(225, 50, 100, 100);
optionLabel.setVisible(true);
option1.setBounds(175, 125, 100, 100);
option1.setSize(200, 50);
option1.setVisible(true);
option2.setBounds(175, 175, 100, 100);
option2.setSize(200, 50);
option2.setVisible(true);
option3.setBounds(175, 225, 100, 100);
option3.setSize(200, 50);
option3.setVisible(true);
optionTextField.setBounds(190, 275, 130, 30);
optionTextField.setVisible(true);
frame.add(optionLabel);
frame.add(option1);
frame.add(option2);
frame.add(option3);
frame.add(optionTextField);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!(textField.getText().isEmpty())) {
input = Integer.parseInt(textField.getText());
if(input >= 1 && input <= 3) {
optionLabel.setVisible(false);
option1.setVisible(false);
option2.setVisible(false);
option3.setVisible(false);
optionTextField.setVisible(false);
}
}
}
});
return input;
}