2017-07-17 72 views
0

Image如何處理java fx 8中的按鈕操作?

在這裏,我想,如果我在textarea上寫了一些東西,然後點擊按鈕,那麼標籤將隨着我輸入的內容而改變。但我無法正確處理代碼。 這裏是我的源代碼--- ClickDemo.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) { 

    } 
} 

Sample1.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="Controllers.Sample1Controller"> 
    <children> 
     <Button fx:id="btn" layoutX="274.0" layoutY="230.0" mnemonicParsing="false" onAction="#btn1handle" text="Button" /> 
     <Label fx:id="label" layoutX="210.0" layoutY="58.0" prefHeight="42.0" prefWidth="233.0" text="This is Text 1"> 
     <font> 
      <Font size="31.0" /> 
     </font> 
     </Label> 
     <TextArea fx:id="textarea" layoutX="101.0" layoutY="121.0" onDragDetected="#textareaHandle" prefHeight="79.0" prefWidth="399.0" /> 
    </children> 
</AnchorPane> 

回答

1

的問題是,你打錯實施的方法稱爲btn1handle。您正在將新偵聽器添加到textarea,而不是將textarea的文本設置到標籤中。方法btn1handle的執行可能非常簡單:

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