项目跳过我的if语句?JAVA

项目跳过我的if语句?JAVA,java,if-statement,Java,If Statement,所以从一开始,我对java是新手。非常新。我决定今晚制作自己的节目,6小时后我就快到了。我的程序是启动一个组合框,这取决于你选择的是什么,当你点击按钮时,它应该设置为一个字符串。然后它比较if语句中的字符串,并根据其为真或假写入文件。我似乎不能让它写得好像这句话是真的。我对调试器有问题。这是我的密码: String[] realms = {"Choose a realm", "True-WoW", "Eternal-WoW"}; JComboBox RealmList = new JComboB

所以从一开始,我对java是新手。非常新。我决定今晚制作自己的节目,6小时后我就快到了。我的程序是启动一个组合框,这取决于你选择的是什么,当你点击按钮时,它应该设置为一个字符串。然后它比较if语句中的字符串,并根据其为真或假写入文件。我似乎不能让它写得好像这句话是真的。我对调试器有问题。这是我的密码:

String[] realms = {"Choose a realm", "True-WoW", "Eternal-WoW"};
JComboBox RealmList = new JComboBox(realms);
JButton button = new JButton("Change Realmlist and Launch WoW");
JLabel label = new JLabel("Realms: ");
JLabel label2 = new JLabel();
JLabel label3 = new JLabel("Made by: Ian D");

String chosenRealm;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    WoWRealmlist classInstance = new WoWRealmlist();
    classInstance.comboBox();
    classInstance.changeRealm();
    //classInstance.runWoW();

} //End of main

public void comboBox() {

    RealmList.setSelectedIndex(0);

    JFrame f = new JFrame("WoW Realm Chooser");
    f.setVisible(true);
    f.setSize(300, 150);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();
    p.add(label);
    p.add(RealmList);
    p.add(button);
    p.add(label3);

    f.add(p);

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            chosenRealm = RealmList.getSelectedItem().toString();
            label2.setText(chosenRealm);


        } //End of actionPerformed

    }); //End of button.addActionListener

} //End of comboBox

public void changeRealm() {

    if ("True-WoW".equals(chosenRealm)) {

        File f = new File("E:\\Prgram Files\\WoW Wrath of the Lich King\\Data\\enUS\\", "realmlist.wtf");
        try {
            FileWriter fw = new FileWriter(f, false);
            fw.flush();
            fw.write("set realmlist login.truewow.org");
            fw.close();

        } catch (Exception ex) {

        } //End of catch

    } //End of if
    else {
        File f = new File("E:\\Prgram Files\\WoW Wrath of the Lich King\\Data\\enUS\\", "realmlist.wtf");
        try {
            FileWriter fw = new FileWriter(f, false);
            fw.flush();
            fw.write("set realmlist logon.eternal-wow.com");
            fw.close();

        } catch (Exception ex) {

        } //End of catch`

chosenRealm
应等于“真哇”,并使我的if语句为真

设置chosenRealm的唯一方法是:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        chosenRealm = RealmList.getSelectedItem().toString();
        label2.setText(chosenRealm);


    } //End of actionPerformed
也就是说,每当你点击这个按钮,它就会设置chosenRealm。您调用
changeRealm
方法,该方法在main中使用if,此时,您的chosenRealm仍将具有默认值,即null,因此您不会输入if条件


我建议您仅在单击按钮然后写入文件时才将调用移动到
changeRealm
方法。

尝试使用调试器调试代码(每个IDE都有一个)。或者,对于start,在
if
语句之前添加
System.out.println(“所选领域:+chosenRealm)
。是否确实要为实例赋值,如果调试器有问题,只需使用print语句手动调试,只需检查分配给您的instancePredrag的值,它会打印“null”,但这不是因为我已经单击了按钮吗?实际上,是:)您应该从按钮的操作侦听器调用
changeRealm()
,然后将其从“main”中删除。所以我这样做了,它一直等到我单击按钮,但是当我在下拉列表中单击true wow,然后单击按钮时,仍然会打印null:(很抱歉没有编程xDSMA的能力,所以你说的是将我的“classInstance.changeRealm();”移到“button.addActionListener(new ActionListener())下面“??”是,仅在为chosenRealm变量赋值后的语句。