[英]Using event handler in JAVA FX
在我們的課程中,我們得到了一個作業,我們應該創建一個比薩餅GUI,使用頂部的復選框和用於比薩餅大小和類型的單選按鈕。
我們已經為GUI創建了基礎,甚至實現了邏輯,但遇到了一個小問題。
在我的程序中,我希望用戶選擇其澆頭,比薩餅大小和比薩餅類型。 用戶完成上述任務后,我希望他們單擊過程選擇,然后將信息以及銷售價格添加到新的文本區域中。
不幸的是,即使在新的textarea中調用字符串(我存放所有內容的字符串)時,我仍然收到空白。
因此,我相信我沒有適當地要求處理程序中的操作。 我也收到警告“未使用事件參數”
我在下面剪切了我的代碼的一部分,如您所見,我試圖將所有數據存儲在ordertext中,然后在新的文本區域orderscreen中調用它。 我希望有人能發現我犯的錯誤,或者讓我對我所忽略的東西有所了解。 謝謝
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
});
我只是解決了問題
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
無需事件處理程序。 在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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.