Java ActionListener中未初始化的变量
我正在尝试让这个程序打印选择的顶部和外壳,按照现在设置的方式,它只打印名称。我尝试将所有字符串初始化为空字符串“”。但无论单击哪个按钮,字符串始终为空,即使不支持使用if语句来更改它们 这是密码Java ActionListener中未初始化的变量,java,awt,actionlistener,Java,Awt,Actionlistener,我正在尝试让这个程序打印选择的顶部和外壳,按照现在设置的方式,它只打印名称。我尝试将所有字符串初始化为空字符串“”。但无论单击哪个按钮,字符串始终为空,即使不支持使用if语句来更改它们 这是密码 public void actionPerformed(ActionEvent e) { if(e.getSource() == output){ String str2, str3, str4, str5; String str1 = txtName.g
public void actionPerformed(ActionEvent e) {
if(e.getSource() == output){
String str2, str3, str4, str5;
String str1 = txtName.getText();
if(e.getSource() == optThick){
str2 = "thick crust";
}
else if (e.getSource() == optThin){
str2 = "thin crust ";
}
if(e.getSource() == cbCheese){
str3 = "Cheese ";
}
if(e.getSource() == cbOlives){
str4 = "Olives ";
}
if(e.getSource() == cbTomatoes){
str5 = "Tomatoes ";
}
textArea.setText("Name : " + str1 + "\n" + "Crust: " + str2 + "\n" + "Toppings: " + str3 + str4 + str5);
}
}
}
所有if语句都在外部语句中。是吗?如果有人想知道,我就是这样让它工作的
public void actionPerformed(ActionEvent e) {
if(e.getSource() == output){
String str2 = "", str3 = "", str4 = "", str5 = "";
String str1 = txtName.getText();
if(optThick.isSelected()){
str2 = "Thick crust";
}
else if (optThin.isSelected()){
str2 = "Thin crust ";
}
if(cbCheese.isSelected()){
str3 = "Cheese ";
}
if(cbOlives.isSelected()){
str4 = "Olives ";
}
if(cbTomatoes.isSelected()){
str5 = "Tomatoes ";
}
textArea.setText(String.format("Name: %s \nCrust: %s\n Toppings: %s%s%s ",str1, str2, str3, str4, str5));
}
}
“optThick”和这些变量的背后是什么?如果你像“==”那样比较它们,你可能只是比较可能失败的对象。调试器说了什么?变量str2、str3、str4和str5可能尚未初始化
public void actionPerformed(ActionEvent e) {
if(e.getSource() == output){
String str2 = "", str3 = "", str4 = "", str5 = "";
String str1 = txtName.getText();
if(optThick.isSelected()){
str2 = "Thick crust";
}
else if (optThin.isSelected()){
str2 = "Thin crust ";
}
if(cbCheese.isSelected()){
str3 = "Cheese ";
}
if(cbOlives.isSelected()){
str4 = "Olives ";
}
if(cbTomatoes.isSelected()){
str5 = "Tomatoes ";
}
textArea.setText(String.format("Name: %s \nCrust: %s\n Toppings: %s%s%s ",str1, str2, str3, str4, str5));
}
}