while循环不会更新变量值

时间:2016-08-05 05:38:58

标签: java swing variables if-statement while-loop

我有一个循环检查变量的值,当我更新变量“i”以匹配其中一个if语句没有任何反应时,任何人都知道为什么会发生这种情况或如何解决它?我有它将“i”变量打印到控制台,所以我可以看到它改变但它不会激活其中一个if语句

package window;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Gui extends JFrame {

public JPanel contentPane;
public static JTextField input;
public static JButton send;
public static JTextArea console;
public static JTextArea invintory;
public static JTextArea stats;

/**
 * Launch the application.
 */



/**
 * Create the frame.
 */
public Gui() {
    //variables

    int Gold = 20;
    int Health = 100;
    int MaxHealth = 100;


    //variables end
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    console = new JTextArea();
    console.setEditable(false);
    console.setBounds(10, 11, 281, 214);
    contentPane.add(console);

    input = new JTextField();
    input.setBounds(10, 236, 281, 20);
    contentPane.add(input);
    input.setColumns(10);

    stats = new JTextArea();
    stats.setEditable(false);
    stats.setBounds(301, 11, 123, 53);
    contentPane.add(stats);

    invintory = new JTextArea();
    invintory.setEditable(false);
    invintory.setBounds(301, 75, 128, 137);
    contentPane.add(invintory);

    send = new JButton("Send");



    send.setBounds(301, 224, 128, 32);
    contentPane.add(send);

    stats.append("Health: " + Health + "/" + MaxHealth + "\n");
    stats.append("Gold: " + Gold);

    }

public static void game (JButton send, JTextArea console, JTextArea stats, JTextArea invintory, JTextField input) {

    //variables

    int Gold = 20;
    int Health = 100;
    int MaxHealth = 100;




    //start
    //START
    //START
    //START
    //START
    while (true) {  
        input.setText("");
        String i = "";
         send.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    String i = input.getText();
                    System.out.println("input is " + i);
                    stats.setText("");
                    stats.append("Health: " + Health + "/" + MaxHealth + "\n");
                    stats.append("Gold: " + Gold);
                }
            });
        console.append("You wake up open feild, with vast amounts of wheet in every direction\n");
        console.append("There is a path going in either direction what do you want to do\n");
        console.append("\t1. Go left.\n");
        console.append("\t2. Go right.\n"); 
        while (i == "") {
        if (i == "1") {
            console.append("1\n");
            break;
        }
        else if (i == "2") {
            console.append("2\n");
            break;
        }
        else {

        }
        }



    }
    //END
    //END
    //END
    //END
    }

public static void main(String[] args) {
            try {
                Gui frame = new Gui();
                frame.setVisible(true);
                Gui.game(send, console, stats, invintory, input);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
}

0 个答案:

没有答案