我正在尝试让这个程序打印选择的顶部和外壳,现在它的设置方式,它只打印名称。我尝试将所有字符串初始化为空字符串“”。但无论点击什么按钮,字符串始终为空,即使if语句被要求更改它们。
这是代码
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);
}
}
}
答案 0 :(得分:1)
if(e.getSource() == output){
// code omitted
}
所有if语句都在outer语句中。是吗?
答案 1 :(得分:0)
如果有人想知道,这就是我开始工作的方式。
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));
}
}