java - 无法访问句柄事件中的按钮/文本区域

标签 java javafx

我刚刚开始使用 JavaFX,并一直在尝试添加一个事件,该事件将在您按下“发送”按钮时将文本添加到文本区域并清除文本字段。但是,我似乎无法在handle方法中检查事件的来源。

我尝试寻找解决方案,但其他人似乎没有遇到同样的问题 - 要么是我错过了一些明显的问题。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ApplicationMain extends Application implements EventHandler<ActionEvent>{

    Stage window;

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

    // Scene Method
    @SuppressWarnings("static-access")
    @Override
    public void start(Stage primaryStage) {

        // Window Stuff
        window = primaryStage;
        window.setTitle("Chat Application");

        // Setup Grid Layout
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.TOP_LEFT);
        grid.setHgap(10);
        grid.setStyle("-fx-background-color: #272828;");

        // MenuBar
        MenuBar menu = new MenuBar();

        menu.setPrefWidth(1000);
        menu.setPrefHeight(20);

        // Creation of File + Help
        Menu file = new Menu("File");
        Menu help = new Menu("Help");

        // Add the Menus to the MenuBar
        menu.getMenus().add(file);
        menu.getMenus().add(help);

        // Add MenuBar to Scene
        menu.setVisible(true);
        grid.add(menu, 0, 0);

        // Text Area Stuff
        TextArea area = new TextArea();

        area.setPrefWidth(1000);
        area.setPrefHeight(700);
        area.setEditable(false);
        area.setStyle("-fx-control-inner-background: #313233;");

        // Add Text Area to Grid
        grid.add(area, 0, 1);

        // Text Field
        TextField enter = new TextField();

        enter.setPromptText("Type here...");
        enter.setMaxWidth(920);
        enter.setMaxHeight(30);
        enter.setStyle("-fx-padding: 5px;");

        // Button
        Button send = new Button("Send!");

        // Set the Handler for the Send Button Event
        send.setOnAction(this);

        // Use of HBox to Space out Text Field & Send Button
        HBox row = new HBox();

        row.setSpacing(10);
        row.setHgrow(enter, Priority.ALWAYS);
        row.getChildren().addAll(enter, send);

        // Use of VBox to Space out Text Field
        VBox box = new VBox();
        box.setSpacing(10);
        box.setPadding(new Insets(10));
        box.getChildren().add(row);

        // Add HBox in VBox to Grid
        grid.add(box, 0, 2);

        // Scene Stuff
        Scene scene = new Scene(grid, 1000, 750);
        window.setScene(scene);

        // Display the Window
        window.show();
    }

    // Event Handler
    @Override
    public void handle(ActionEvent event) {

        if (event.getSource() == send) {

        }
    }
}

每当我尝试检查源是否是“发送”按钮时,它都不会显示 - 就好像该方法无法访问它一样。我不确定如何解决这个问题。

最佳答案

这段代码有一些问题,但我们可以毫无问题地修复它。

首先学习命名约定并坚持使用它们,正如 @kleopatra 所说,如果你用 google 搜索 java 命名约定,你会因阅读一些结果而不堪重负

接下来你不应该调用 Stage一个window已经有另一个对象具有该名称,因此它可能会让其他人感到困惑,但如果它只适合您,我想没关系

我不会@SuppressWarnings("static-access")正如您所做的那样,如果您有错误修复,请不要忽略它

send.setOnAction(this);不是处理事件的方法删除您的 implements EventHandler<ActionEvent>您可以通过这样设置来使用事件处理程序

send.setOnAction(event -> sendToTextArea(enter.getText(), area));

这就是您调用的方法应该是什么样的

private void sendToTextArea(String string, TextArea textArea){
    //textArea.setText(string);Use setText if you want to set the whole area to something
    textArea.appendText(string+"\n");//Use appendText to append add new line because chat app
}

其他一切看起来都不错,这就是您的最终产品应该是什么样的

public class Main extends Application {

    private Stage stage;

    @Override
    public void start(Stage primaryStage) {
        // stage Stuff
        stage = primaryStage;
        stage.setTitle("Chat Application");

        // Setup Grid Layout
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.TOP_LEFT);
        grid.setHgap(10);
        grid.setStyle("-fx-background-color: #272828;");

        // MenuBar
        MenuBar menu = new MenuBar();

        menu.setPrefWidth(1000);
        menu.setPrefHeight(20);

        // Creation of File + Help
        Menu file = new Menu("File");
        Menu help = new Menu("Help");

        // Add the Menus to the MenuBar
        menu.getMenus().add(file);
        menu.getMenus().add(help);

        // Add MenuBar to Scene
        menu.setVisible(true);
        grid.add(menu, 0, 0);

        // Text Area Stuff
        TextArea area = new TextArea();

        area.setPrefWidth(1000);
        area.setPrefHeight(700);
        area.setEditable(false);
        area.setStyle("-fx-control-inner-background: #313233;");

        // Add Text Area to Grid
        grid.add(area, 0, 1);

        // Text Field
        TextField enter = new TextField();

        enter.setPromptText("Type here...");
        enter.setMaxWidth(920);
        enter.setMaxHeight(30);
        enter.setStyle("-fx-padding: 5px;");

        // Button
        Button send = new Button("Send!");

        // Set the Handler for the Send Button Event
        send.setOnAction(event -> sendToTextArea(enter, area));

        // Use of HBox to Space out Text Field & Send Button
        HBox row = new HBox();

        row.setSpacing(10);
        row.setHgrow(enter, Priority.ALWAYS);
        row.getChildren().addAll(enter, send);

        // Use of VBox to Space out Text Field
        VBox box = new VBox();
        box.setSpacing(10);
        box.setPadding(new Insets(10));
        box.getChildren().add(row);

        // Add HBox in VBox to Grid
        grid.add(box, 0, 2);

        // Scene Stuff
        Scene scene = new Scene(grid, 1000, 750);
        stage.setScene(scene);

        // Display the stage
        stage.show();
    }

    private void sendToTextArea(TextField textField, TextArea textArea){
        //textArea.setText(string);Use setText if you want to set the whole area to something
        //textArea.clear();and .clear to clear all text from the TextArea
        textArea.appendText(textField.getText()+"\n");//Use appendText to append add new line because chat app
        textField.clear();
    }

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

关于java - 无法访问句柄事件中的按钮/文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55539195/

相关文章:

java - GATE 数字、货币、金钱的标记注释

java - 如何使用 Javafx 显示我在选项卡 Pane 中选择的文件的内容?

java - 如何将两个场景合并为一个场景?

multithreading - 重新实现 JavaFX showAndWait

java - 在 Java 中找不到指定 id 的上下文 - Selenium webdriver

java - 在 Java 中将视频帧保存为静态图像

java - 如何设置缓存gradle文件的路径?

java - 如何检查一个整数是否可以被3整除

java - 如何解决JavaFx自定义时间线问题?

java - 如何仅在 TreeTableView 中按叶子排序?