【问题标题】:Javafx and fxml button not clickableJavafx 和 fxml 按钮不可点击
【发布时间】:2017-10-16 05:36:41
【问题描述】:

我有一个混淆的代码,我正在使用带有启动画面的 Scene Builder 构建程序。我在作为根的锚窗格顶部的 TitledPane 上构建了一个按钮。我设置了 fx:id 并且当我运行时,我无法单击任何内容。甚至没有标题窗格中的选项卡或任何按钮。有趣的是,我可以用 tab 键遍历并单击空间来实际单击它。发生了什么事,我应该怎么做才能解决这个问题?

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;

/** * * @作者 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>

【问题讨论】:

    标签: button javafx click fxml traversal


    【解决方案1】:

    这是因为您在TitledPane 中设置了属性mouseTransparent="true"

    在 JavaFX 中,MouseTransparent 属性使任何节点及其所有子节点忽略任何鼠标交互,这就是您无法单击按钮的原因。只需将其删除即可。

    Node - MouseTransparentProperty

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 2015-09-26
      • 2017-06-12
      • 2019-10-27
      • 1970-01-01
      • 2015-10-05
      相关资源
      最近更新 更多