2017-10-16 240 views
0

我有一個混合代碼,我正在使用帶有Splash Screen的Scene Builder構建程序。我在錨窗格頂部的一個TitledPane上創建了一個按鈕,它是根。我設置了fx:id,當我運行時,我無法點擊任何東西。甚至沒有標題窗格或任何按鈕的選項卡。有趣的是,我可以用tab鍵遍歷並點擊空間來實際點擊它。發生了什麼,我應該怎麼做才能解決這個問題?Javafx和fxml按鈕不可點擊

import java.io.File; 
import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.animation.FadeTransition; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.ListView; 
import javafx.scene.control.ProgressBar; 
import javafx.scene.control.TitledPane; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.FileChooser; 
import javafx.util.Duration; 

/** * * @author heecheonpark */ 公共類FXMLDocumentController實現Initializable {

@FXML 
private Label label; 

@FXML 
private AnchorPane root; 

@FXML 
private TitledPane tPane; 

@FXML 
private AnchorPane aPane1; 

@FXML 
private ListView listView; 

@FXML 
private Button addBtn; 

@FXML 
private void addButtonAction(ActionEvent event) { 
      FileChooser fc = new FileChooser(); 
      File selectedFile = fc.showOpenDialog(null); 
      if (selectedFile != null){ 
       listView.getItems().add(selectedFile.getName()); 
      } 
      else{ 
       System.out.println("The file is not valid."); 
      } 
} 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
    if (!MajorProject.isSplashLoaded){ 
    loadSplashScreen(); 

    } 
}  
private void loadSplashScreen(){ 
    try { 
     AnchorPane aPane = FXMLLoader.load(getClass().getResource("SplashFXML.fxml")); 
     root.getChildren().addAll(aPane); 
     FadeTransition fadeIn = new FadeTransition(Duration.seconds(3), aPane); 
     fadeIn.setFromValue(0); 
     fadeIn.setToValue(1); 
     fadeIn.setCycleCount(1); 

     FadeTransition fadeOut = new FadeTransition(Duration.seconds(3), aPane); 
     fadeOut.setFromValue(1); 
     fadeOut.setToValue(0); 
     fadeOut.setCycleCount(1); 

     fadeIn.play(); 

     fadeIn.setOnFinished((e) -> { 
     fadeOut.play(); 
     MajorProject.isSplashLoaded = true; 
     }); 

     fadeOut.setOnFinished((e) -> { 
      try { 
       Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 


      } catch (IOException ex) { 
       Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     }); 

    } catch (IOException ex) { 
     Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

}

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

<?import javafx.scene.effect.*?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane fx:id="root" focusTraversable="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="majorproject.FXMLDocumentController"> 
    <children> 
     <ListView fx:id="listView" layoutX="302.0" layoutY="53.0" prefHeight="304.0" prefWidth="248.0"> 
     <effect> 
      <Glow /> 
     </effect> 
     </ListView> 
     <TitledPane fx:id="tPane" animated="false" mouseTransparent="true" text="Product"> 
     <content> 
      <AnchorPane fx:id="aPane1"> 
       <children> 
        <Button layoutY="131.0" mnemonicParsing="true" text="Button" /> 
        <Button fx:id="addBtn" layoutY="104.0" mnemonicParsing="false" onAction="#addButtonAction" text="Add" /> 
       </children> 
      </AnchorPane> 
     </content> 
     </TitledPane> 
    </children> 
</AnchorPane> 

回答

0

這是因爲你設置的屬性mouseTransparent="true"你的TitledPane

在JavaFX中,MouseTransparent屬性使任何節點和他所有的子女都不理會任何鼠標交互,這就是爲什麼你不能點擊按鈕。只要刪除它,它應該工作。

Node - MouseTransparentProperty