[英]How do I return a pane?
我這里的程序幾乎完成了。 它是其中的一個帶有事件處理程序和更新總數的工作訂單表格。 收到用戶輸入后如何返回更新后的總值? 我確定我在某處需要返回窗格,只是不確定在哪里。 代碼如下:
public class Working_order extends Application {
// Radio buttons
private RadioButton rbNext = new RadioButton("$20");
private RadioButton rbThis = new RadioButton("$12");
private RadioButton rbSome = new RadioButton("$5");
private Label lbDue = new Label("$0.00");
@Override
public void start(Stage primaryStage) {
// Create a pane and set its properties
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
// Place nodes in the pane
pane.add(new Label("Item"), 0, 0);
pane.add(tfItem, 1, 0);
pane.add(new Label("Price"), 0, 1);
pane.add(tfPrice, 1, 1);
pane.add(new Label("Quantity"), 0, 2);
pane.add(tfQty, 1, 2);
CheckBox chTaxable = new CheckBox("Taxable?");
pane.add(chTaxable, 1, 3);
// More nodes in a pane
pane.add(new Label("Shipping"), 0, 4);
pane.add(rbNext, 1, 5);
pane.add(new Label("Next Day"), 0, 5);
pane.add(rbThis, 1, 6);
pane.add(new Label("This Week"), 0, 6);
pane.add(rbSome, 1, 7);
pane.add(new Label("Total Due"), 0, 8);
pane.add(lbDue, 1, 8);
pane.add(new Label("Some Day"), 0, 7);
Button btAdd = new Button("Process");
Button btAdd2 = new Button("Reset");
// Toggle group
ToggleGroup group = new ToggleGroup();
rbNext.setToggleGroup(group);
rbThis.setToggleGroup(group);
rbSome.setToggleGroup(group);
btAdd.setOnAction(e -> {
// read textboxes
String sPrice = tfPrice.getText();
double price = Double.parseDouble(sPrice);
int qty = Integer.parseInt(tfQty.getText());
double subTotal = price * qty;
double tax;
if (chTaxable.isSelected()) {
tax = subTotal * 0.07;
} else {
tax = 0;
}
});
pane.add(btAdd, 0, 9);
pane.add(btAdd2, 1, 9);
GridPane.setHalignment(btAdd, HPos.RIGHT);
GridPane.setHalignment(btAdd2, HPos.LEFT);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowGridPane"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display
}
public static void main(String[] args) {
launch(args);
}
}
首先,您沒有構造文本字段(tfItem,tfPrice,tfQty)。 為了解決這個問題,添加這些行
TextField tfItem = new TextField();
TextField tfPrice = new TextField();
TextField tfQty = new TextField();
您的問題很簡單,通過在btAdd (進程按鈕)事件處理程序中應用公式來計算結果后,只需在事件處理程序的末尾添加此行
lbDue.setText( result + "" );
這將解決您的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.