![](/img/trans.png)
[英]I am making a GUI based text adventure game in Java. How do I make the message boxes update to show the outcomes?
[英]I'm trying to make a text based GUI game in Java. How do I make the message boxes update to show the next outcomes?
我現在添加了更多提示,消息框應該說不同的內容,但是它們停留在“您向前撞牆”和“您現在正面臨相反的方向”上,它們在應該更改時不會改變,而第三個選項我添加的沒有顯示。 再次,這是我的代碼:
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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.