首页 > 解决方案 > 尝试将按钮添加到 JavaFX 中的 VBox

问题描述

我正在尝试为石头剪刀布创建一个简单的程序,但我无法在窗格中添加任何内容。我当我做 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");
    }
}

标签: javaintellij-ideajavafx

解决方案


您正在使用 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 内的其他容器中,或者将其添加到根目录。


推荐阅读