如何在JavaFX8中处理按钮操作?

如何在JavaFX8中处理按钮操作?,java,javafx-8,scenebuilder,Java,Javafx 8,Scenebuilder,在这里,我想,如果我写了一些东西在文本区,然后点击按钮,然后标签将改变我键入的内容。但是我不能正确地处理代码。 这是我的源代码--- 单击demo.java- package Application; import java.io.FileInputStream; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.fxml.FXMLL

在这里,我想,如果我写了一些东西在文本区,然后点击按钮,然后标签将改变我键入的内容。但是我不能正确地处理代码。 这是我的源代码--- 单击demo.java-

package Application;

import java.io.FileInputStream;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


public class ClickDemo extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        // Path to the FXML File
        String fxmlDocPath = "src/View/sample1.fxml";
        FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);

        // Create the Pane and all Details
        AnchorPane root = (AnchorPane) loader.load(fxmlStream);

        // Create the Scene
        Scene scene = new Scene(root);
        // Set the Scene to the Stage
        stage.setScene(scene);
        // Set the Title to the Stage
        stage.setTitle("A SceneBuilder Example");
        // Display the Stage
        stage.show();
    }


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

}
Sample1Controller.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;

/**
 * FXML Controller class
 *
 * @author Dell
 */
public class Sample1Controller {

    /**
     * Initializes the controller class.
     */
    @FXML
    private Button btn;
    @FXML
    private Label label;
    @FXML
    private TextArea textarea;

    @FXML
    void btn1handle(ActionEvent event) {

        // label.setText("Hello world");
        textarea.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                //System.out.println(newValue);
                label.setText(newValue);
            }

        });
    }

    @FXML
    void textareaHandle(MouseEvent event) {

    }
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装控制器;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextArea;
导入javafx.scene.input.MouseEvent;
/**
*FXML控制器类
*
*@作者戴尔
*/
公共类采样控制器{
/**
*初始化控制器类。
*/
@FXML
专用按钮btn;
@FXML
自有品牌;
@FXML
私人文本区文本区;
@FXML
无效btn1handle(ActionEvent事件){
//label.setText(“Hello world”);
textarea.textProperty().addListener(新的ChangeListener(){
@凌驾
公共无效已更改(可观察值)

问题在于,名为btn1handle的方法的实现错误。您将新的侦听器添加到textarea,而不是将textarea的文本设置到标签中。方法btn1handle的实现可能非常简单:

@FXML
void btn1handle(ActionEvent event) {
    label.setText(textarea.getText());
}
@FXML
void btn1handle(ActionEvent event) {
    label.setText(textarea.getText());
}