I';我正在尝试用Java制作一个基于文本的GUI游戏。如何更新消息框以显示下一个结果?
我现在添加了更多的提示,消息框应该会说不同的话,但它们被困在“你向前撞到了墙”和“你现在面对的是相反的方向”上,它们不会在应该的时候改变,我添加的第三个选项也不会出现。同样,这是我的代码:I';我正在尝试用Java制作一个基于文本的GUI游戏。如何更新消息框以显示下一个结果?,java,swing,Java,Swing,我现在添加了更多的提示,消息框应该会说不同的话,但它们被困在“你向前撞到了墙”和“你现在面对的是相反的方向”上,它们不会在应该的时候改变,我添加的第三个选项也不会出现。同样,这是我的代码: package game; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class game extends JFrame implements
package game;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class game extends JFrame implements ActionListener {
JLabel prompt;
JTextField name;
JTextField name1;
JButton click;
JButton click1;
String storeName;
String storeName1;
JButton click2;
public game(){
setLayout(null);
setSize(300,250);
setTitle("Text Adventure");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prompt = new JLabel("You find yourself in a tunnel.");
click = new JButton("Go forward!");
click1 = new JButton("Turn around!");
click2 = new JButton();
name = new JTextField();
name1 = new JTextField();
prompt.setBounds(60,30,1300,30);
click.setBounds(50,130,100,30);
click.addActionListener(this);
click1.setBounds(150,130,125,30);
click1.addActionListener(this);
click2.setBounds(125,160,125,30);
click2.addActionListener(this);
add(click);
add(click1);
add(name);
add(name1);
add(prompt);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == click) {
JOptionPane.showMessageDialog(null, "You go forward and hit a wall.");
}
{
if(e.getSource() == click1) {
JOptionPane.showMessageDialog(null, "You are now facing the opposite direction.");
prompt.setText("What would you like to do now?");
click.setText("Go forward!");
click1.setText("Turn on light.");
}
}
}
public void actionPerformed1(ActionEvent e) {
if(e.getSource() == click) {
JOptionPane.showMessageDialog(null, "You walk down the tunnel until you hit an intersection.");
prompt.setText("Which way would you like to go?");
click.setText("Left!");
click1.setText("Right!");
add(click2);
click2.setText("Keep going!");
}
if(e.getSource() == click1) {
}
}
public static void main(String args[]){
game s = new game();
s.setVisible(true);
}
}
在actionPerformed函数中,使用
prompt.setText(" new prompt");
将提示设置为其他提示
您还可以通过以下方式更改选项:
click.setText(" new click command");
click1.setText(" new click1 command");
如何将不能选择的选项标记为禁用()或使其不可见()?这可能是旧选项的重复。