项目跳过我的 if 语句? Java
project skipping my if statement? Java
所以刚开始,我是 java 的新手。很新。我决定今晚制作自己的节目,6 小时后我几乎完成了。我的程序是启动一个组合框,具体取决于您 select 的内容,当您单击该按钮时,它应该将其设置为一个字符串。然后它比较 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
应该等于 "True-WoW" 并使我的 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
方法。
所以刚开始,我是 java 的新手。很新。我决定今晚制作自己的节目,6 小时后我几乎完成了。我的程序是启动一个组合框,具体取决于您 select 的内容,当您单击该按钮时,它应该将其设置为一个字符串。然后它比较 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
应该等于 "True-WoW" 并使我的 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
方法。