gpt4 book ai didi

java - 尝试向 JavaFX 中的 VBox 添加按钮

转载 作者:行者123 更新时间:2023-12-02 01:33:12 24 4
gpt4 key购买 nike

我正在尝试为石头剪刀布创建一个简单的程序,但在向 Pane 中添加任何内容时遇到问题。当我执行 pane.getChildren().add(goButton); 时它无法编译并表示无法应用。我确信它很简单,但我的代码与我在网上看到的代码看起来是一样的。

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.*;
import java.awt.*;

public class Main extends Application {

// private String[] answers = {"rock", "paper", "scissors"};
// private TextField userOutputTF, compOutputTF;
// private Button goButton;
// private Label title;
// private VBox pane;

@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.setTitle("Rock, Paper, Scissors");

VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);

String[] answers = {"rock", "paper", "scissors"};

Button goButton = new Button("Go");
pane.getChildren().add(goButton);

TextField userOutputTF = new TextField("Enter rock, paper, or scissors");
TextField compOutputTF = new TextField("");

compOutputTF.setEditable(false);
userOutputTF.setEditable(true);

goButton.addActionListener(e -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});

primaryStage.show();
}


public static void main(String[] args) {
launch(args);
}


public void Deal() {
System.out.println("Hi");
}
}

最佳答案

您正在使用 AWT 控件。更改导入以使用 javafx 控件,您还需要更改按钮监听器:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class Sample extends Application {

// private String[] answers = {"rock", "paper", "scissors"};
// private TextField userOutputTF, compOutputTF;
// private Button goButton;
// private Label title;
// private VBox pane;

@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.setTitle("Rock, Paper, Scissors");

VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);

String[] answers = {"rock", "paper", "scissors"};

Button goButton = new Button("Go");
pane.getChildren().add(goButton);

TextField userOutputTF = new TextField("Enter rock, paper, or scissors");
TextField compOutputTF = new TextField("");

compOutputTF.setEditable(false);
userOutputTF.setEditable(true);

goButton.onMouseClickedProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});

primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}


public void Deal() {
System.out.println("Hi");
}
}

您还需要将 VBox 添加到 FXML 内的其他容器或仅将其添加到根目录。

关于java - 尝试向 JavaFX 中的 VBox 添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55698909/

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