首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正在尝试将按钮添加到JavaFX中的VBox

正在尝试将按钮添加到JavaFX中的VBox
EN

Stack Overflow用户
提问于 2019-04-16 08:22:49
回答 1查看 1.5K关注 0票数 0

我正在尝试创建一个简单的程序为岩石剪布剪刀,但我有困难添加任何东西到窗格。当我执行pane.getChildren().add(GoButton)时,它不能编译,并且说它不能被应用。我确信这很简单,但我的代码与我在网上看到的代码看起来是一样的。

代码语言:javascript
复制
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");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-16 08:41:49

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

代码语言:javascript
复制
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内的其他容器中,或者仅将其添加到根目录。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55698909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档