首先,我是java的新手。很新。我决定今晚制作自己的节目,6小时后我几乎到了那里。我的程序是启动一个组合框,取决于你选择哪个东西,当你点击按钮时,它应该设置为一个字符串。然后它比较if语句中的字符串,并根据它为true或false写入文件。期待我似乎不能写它好像声明是真的。我有调试器的问题。这是我的代码:
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语句成立。
答案 0 :(得分:2)
您设置selectedRealm的唯一方法是:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chosenRealm = RealmList.getSelectedItem().toString();
label2.setText(chosenRealm);
} //End of actionPerformed
这意味着,无论何时单击按钮,它都会设置selectedRealm。并且您调用changeRealm
方法,该方法在main中使用,并且在那个时间点,您的selectedRealm仍然具有默认值,即null,因此您不会输入if条件。
我建议您仅在单击按钮然后写入文件时才调用changeRealm
方法。