在我们班上,我们已经给出了一项任务,我们应该使用配料复选框和披萨大小和类型的单选按钮创建披萨GUI。
我们已经为我的GUI创建了基础,我甚至实现了逻辑,但我遇到了一个小问题。
在我的程序中,我希望用户选择他们的浇头,披萨大小和披萨类型。用户完成上述任务后,我希望他们点击流程选择,并将信息和销售价格添加到新的文本区域。
不幸的是,即使在新文本区域中调用字符串(我保存所有内容)时,我仍然会收到一个空格。
因此,我不得不相信我没有在处理程序中正确地要求操作。我还收到一条未使用的警告"事件参数"
我已经删除了下面代码的snipet,因为你可以看到我试图将所有数据存储在ordertext中,然后在我的新文本区域ordercreen中调用它。我希望有人能够发现我所犯的错误,或者让我对我忽略的内容有所了解。谢谢
TextArea orderscreen = new TextArea();
orderscreen.setPrefColumnCount(50);
orderscreen.setPrefRowCount(7);
grid.add(orderscreen, 0, 4);
orderscreen.setText(ordertext);
btn.setOnAction((ActionEvent event) -> {
String mytoppings = "";
double mytopcost = 0.0;
if (chkTom.isSelected()) {
mytoppings = mytoppings + "Tomato "; // Topping
mytopcost += 1.50; // price
}
if (chkGP.isSelected()) {
mytoppings = mytoppings + "Green Peppers "; // Topping
mytopcost += 1.50; // pice
}
if (chkBO.isSelected()) {
mytoppings = mytoppings + "Black Olives "; // Topping
mytopcost += 1.50; // pice
}
if (chkMR.isSelected()) {
mytoppings = mytoppings + "MushRooms "; // Topping
mytopcost += 1.50; // pice
}
if (chkEC.isSelected()) {
mytoppings = mytoppings + "Extra Cheese "; // Topping
mytopcost += 1.50; // pice
}
if (chkPep.isSelected()) {
mytoppings = mytoppings + "Peppeoni "; // Topping
mytopcost += 1.50; // pice
}
if (chkSS.isSelected()) {
mytoppings = mytoppings + "Sausage "; // Topping
mytopcost += 1.50; // pice
}
else {
mytoppings = mytoppings + "No toppings selected ";
}
//Pizza Types
String mypizzatype = "";
// rbTC.setOnAction(e -> {
if (rbTC.isSelected()) {
mypizzatype = mypizzatype + "Thin Crust "; // Type
}
// });
//rbMC.setOnAction(e -> {
if (rbMC.isSelected()) {
mypizzatype = mypizzatype + "Medium Crust "; // Type
}
// });
if (rbP.isSelected()) {
mypizzatype = mypizzatype + "Pan "; // Type
}
// PIZZA SIZES
String mypizzasize = "";
Double smpzcost = 6.50;
Double mdpzcost = 8.50;
Double lgpzcost = 10.00;
if (rbSM.isSelected()) {
mypizzatype = mypizzasize + "Small "; // Type
order = smpzcost;
}
if (rbMD.isSelected()) {
mypizzatype = mypizzasize + "Medium "; // Type
order = mdpzcost;
}
if (rbLG.isSelected()) {
mypizzatype = mypizzasize + "Large "; // Type
order = lgpzcost;
}
ordertext = ("Your Order: "
+ "\nPizza type: " + mypizzatype
+ "\nPizza Size: " + mypizzasize
+ "\nToppings: " + mytoppings
+ "\nAmount Due: " + (order + mytopcost));
System.out.println("Order Processed");
//orderscreen.clear(); // WILL CLEAR
});
答案 0 :(得分:0)
我刚刚解决了问题
orderscreen.setText(ordertext);
需要更换
ordertext = ("Your Order: "
+ "\nPizza type: " + mypizzatype
+ "\nPizza Size: " + mypizzasize
+ "\nToppings: " + mytoppings
+ "\nAmount Due: " + (order + mytopcost));
orderscreen.setText(ordertext);
我还需要改变
mypizzatype = mypizzasize
至mypizzasize= mypizzasize
答案 1 :(得分:0)
不需要事件处理程序。在javafx中,您可以使用绑定。
我创建了一个简单的演示,演示了如何将gui控件(RadioButtons
,ChoiceBox
)绑定到模型类中的属性并将其绑定到TextArea
:
package demo;
import javafx.application.Application;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
class User {
private StringProperty order = new SimpleStringProperty();
public String getOrder() {
return order.get();
}
public void setOrder(String order) {
this.order.set(order);
}
public StringProperty orderProperty() {
return order;
}
}
public class Demo extends Application {
private User user = new User();
@Override
public void start(Stage stage) throws Exception {
RadioButton tomatoButton = new RadioButton("Tomato");
RadioButton pepperButton = new RadioButton("Pepper");
RadioButton mushroomButton = new RadioButton("Mushrooms");
ChoiceBox<String> pizzaType = new ChoiceBox<String>();
pizzaType.getItems().addAll("", "Small", "Medium", "Large");
pizzaType.getSelectionModel().selectFirst();
HBox topHBox = new HBox(15.0, tomatoButton, pepperButton, mushroomButton, pizzaType);
// create custom Binding that binds selection of radio buttons and choice box
StringBinding orderBinding = createOrderBinding(tomatoButton.selectedProperty(), pepperButton.selectedProperty(), mushroomButton.selectedProperty(), pizzaType.getSelectionModel().selectedItemProperty());
// bind orderBinding to orderProperty of User
user.orderProperty().bind(orderBinding);
TextArea orderArea = new TextArea();
// bind orderProperty of User to textProperty of TextArea
orderArea.textProperty().bindBidirectional(user.orderProperty());
BorderPane root = new BorderPane();
root.setTop(topHBox);
root.setCenter(orderArea);
Scene scene = new Scene(root, 400, 300);
stage.setScene(scene);
stage.show();
}
/**
* Creates StringBinding between 4 provided arguments. Binding means that when value of one bound property is changed the whole binding is recomputed in computeValue method.
* The value of computeValue is bound to User.orderProperty
*/
public StringBinding createOrderBinding(BooleanProperty tomato, BooleanProperty pepper, BooleanProperty mushroom, ReadOnlyObjectProperty<String> selectedPizzaType) {
StringBinding binding = new StringBinding() {
{
// bind 4 provided properties.
super.bind(tomato, pepper, mushroom, selectedPizzaType);
}
/*
* Fires each time bound property is modified.
*/
@Override
protected String computeValue() {
StringBuilder sb = new StringBuilder("Pizza content:\n");
if (tomato.get())
sb.append("\tTomato\n");
if (pepper.get())
sb.append("\tPepper\n");
if (mushroom.get())
sb.append("\tMushroom\n");
sb.append("Pizza type:\n").append("\t" + selectedPizzaType.get());
return sb.toString();
}
};
return binding;
}
public static void main(String[] args) {
Application.launch(args);
}
}