java - 尝试将按钮添加到 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");
}
}
解决方案
您正在使用 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 内的其他容器中,或者将其添加到根目录。
推荐阅读
- python - 如何以编程方式确定 pdf 是否受版权保护或字符是否加密?
- python - 在 Triton 推理服务器上使用 python 在模型中执行得到问题 json 解析器
- python - Python 正则表达式在后面查找列名
- nginx - 使用 API 网关 Ocelot 与 Nginx 的微服务
- image-processing - Image - detect low contrast edge
- javascript - 无法从 openWeatherMap Api 渲染 weather[0].main 属性,它给出了未定义的错误?
- python - 将文本框中的字符串与文件一起发送到 Flask
- xcode - Flutter 生成的 ios 文件版本控制
- python - 如何从亚马逊加拿大抓取数据?
- java - 线程无法读取正在被其他线程修改的实例