gpt4 book ai didi

java - Gui 按钮不会点击 javaFX

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:13 25 4
gpt4 key购买 nike

我的项目是一个计算器。我已经提供了将 0 注册到 TextField 的 0 按钮功能,但它不起作用。聪明人有解决办法吗?

public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("This is a caculator");

TextArea result = new TextArea("This is where the result will be"); result.setPrefSize(480,50); result.setLayoutX(10); result.setLayoutY(65);
TextField textField = new TextField(); textField.setPrefSize(480,50); textField.setLayoutX(10); textField.setLayoutY(10);

Button button1 = new Button("1"); button1.setPrefSize(100,50); button1.setTranslateX(30); button1.setTranslateY(140);
Button button2 = new Button("2"); button2.setPrefSize(100,50); button2.setTranslateX(150); button2.setTranslateY(140);
Button button3 = new Button("3"); button3.setPrefSize(100,50); button3.setTranslateX(270); button3.setTranslateY(140);
Button button4 = new Button("4"); button4.setPrefSize(100,50); button4.setTranslateX(30); button4.setTranslateY(210);
Button button5 = new Button("5"); button5.setPrefSize(100,50); button5.setTranslateX(150); button5.setTranslateY(210);
Button button6 = new Button("6"); button6.setPrefSize(100,50); button6.setTranslateX(270); button6.setTranslateY(210);
Button button7 = new Button("7"); button7.setPrefSize(100,50); button7.setTranslateX(30); button7.setTranslateY(280);
Button button8 = new Button("8"); button8.setPrefSize(100,50); button8.setTranslateX(150); button8.setTranslateY(280);
Button button9 = new Button("9"); button9.setPrefSize(100,50); button9.setTranslateX(270); button9.setTranslateY(280);
Button button0 = new Button("0"); button0.setPrefSize(100,50); button0.setTranslateX(150); button0.setTranslateY(350);
Button buttonPlus = new Button("+"); buttonPlus.setPrefSize(100,50); buttonPlus.setTranslateX(390); buttonPlus.setTranslateY(210);
Button buttonMinus = new Button("-"); buttonMinus.setPrefSize(100,50); buttonMinus.setTranslateX(390); buttonMinus.setTranslateY(280);
Button buttonMulti = new Button("*"); buttonMulti.setPrefSize(100,50); buttonMulti.setTranslateX(390); buttonMulti.setTranslateY(350);
Button buttonDivide = new Button("/"); buttonDivide.setPrefSize(100,50); buttonDivide.setTranslateX(270);buttonDivide.setTranslateY(350);
Button buttonClear = new Button("C"); buttonClear.setPrefSize(100,50); buttonClear.setTranslateX(390); buttonClear.setTranslateY(140);
Button buttonResult = new Button("="); buttonResult.setPrefSize(100,50); buttonResult.setTranslateX(30); buttonResult.setTranslateY(350);
Button virus = new Button("Install the deadly virus"); virus.setPrefSize(450,50); virus.setTranslateX(30);virus.setTranslateY(430);

Pane pane = new Pane(textField,result,button1,button2,button3,buttonClear,button4,button5,button6,buttonPlus,button7,button8,button9,buttonMinus,buttonMulti,buttonDivide,button0,buttonResult,virus);

button0.setOnAction(event -> textField.setText("0"));

Scene scene = new Scene(pane,500,500);
scene.getStylesheets().add("sample/css/Style.css");
primaryStage.setScene(scene);
primaryStage.show();
}

最佳答案

好吧,我假设你想附加它,连接,将它添加到末尾(无论如何都相同的意思)

button0.setOnAction(e -> button0.appendText("0");
// // // OR // // //
button0.setOnAction(e -> button0.setText(button0.getText() + "0");

两者都会给出相同的结果,但我更喜欢第一个,更优雅。

关于java - Gui 按钮不会点击 javaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com