2016-06-28 177 views
1

对于一个大学项目,我必须创建一个桌面游戏的在线版本的GUI,玩家可以通过扩展JavaFX ImageView创建一些卡片,并且我想将它们放入内部运行时的界面。ListView或ScrollPane滚动条不滚动

因此,我在SplitPane中插入了一个JavaFX ListView,但是当我运行我的应用程序时滚动条不起作用。

卡片完美显示,滚动条可见,但是当我尝试使用它时,它看起来就像一个禁用的按钮。

如果我使用包含HBox的ScrollPane,也会发生同样的情况。

我试图不使用首选大小,我试过ScrollBarPolicies,似乎没有任何工作,最后我得出结论,问题是在SplitPane里面的某个地方。

我甚至想过,也许JavaFX应用程序线程停止,但当滚动条不起作用时,它实际上正在运行。

FXML

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

<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.ListView?> 
<?import javafx.scene.control.SplitPane?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.Pane?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import javafx.scene.text.Font?> 

<SplitPane fx:id="mainPlayerRootPane" disable="true" dividerPositions="0.10275689223057644" prefHeight="337.0" styleClass="mainPlayerRootPane" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.view.GUI.MainPlayerPaneController"> 
    <items> 
     <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"> 
     <children> 
      <GridPane prefHeight="335.0" prefWidth="78.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
       <columnConstraints> 
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" /> 
       </columnConstraints> 
       <rowConstraints> 
        <RowConstraints maxHeight="116.0" minHeight="10.0" prefHeight="80.0" vgrow="SOMETIMES" /> 
        <RowConstraints maxHeight="118.0" minHeight="10.0" prefHeight="82.0" vgrow="SOMETIMES" /> 
        <RowConstraints maxHeight="138.0" minHeight="10.0" prefHeight="86.0" vgrow="SOMETIMES" /> 
        <RowConstraints maxHeight="85.0" minHeight="10.0" prefHeight="85.0" vgrow="SOMETIMES" /> 
       </rowConstraints> 
       <children> 
        <Pane prefHeight="85.0" translateX="4.0"> 
        <children> 
         <ImageView fitHeight="77.0" fitWidth="73.0" layoutX="5.0" layoutY="1.0" pickOnBounds="true" preserveRatio="true"> 
          <image> 
           <Image url="@../../../../resources/images/VictoryPoints.png" /> 
          </image> 
         </ImageView> 
         <Label fx:id="victoryPointsLabel" alignment="TOP_CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="79.0" prefWidth="70.0" text="99" textAlignment="JUSTIFY" textFill="WHITE"> 
          <font> 
           <Font name="Trattatello" size="36.0" /> 
          </font> 
         </Label> 
        </children> 
        </Pane> 
        <Pane prefHeight="85.0" translateX="4.0" GridPane.rowIndex="1"> 
        <children> 
         <ImageView fitHeight="79.0" fitWidth="71.0" layoutY="4.0" pickOnBounds="true" preserveRatio="true"> 
          <image> 
           <Image url="@../../../../resources/images/Coins.png" /> 
          </image> 
         </ImageView> 
         <Label fx:id="coinsLabel" alignment="CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="82.0" prefWidth="77.0" text="20" textFill="#222020"> 
          <font> 
           <Font name="Trattatello" size="30.0" /> 
          </font> 
         </Label> 
        </children> 
        </Pane> 
        <Pane prefHeight="85.0" prefWidth="81.0" translateX="4.0" GridPane.rowIndex="2"> 
        <children> 
         <ImageView fitHeight="79.0" fitWidth="73.0" layoutX="17.0" layoutY="2.0" pickOnBounds="true" preserveRatio="true"> 
          <image> 
           <Image url="@../../../../resources/images/Servants.png" /> 
          </image> 
         </ImageView> 
         <Label fx:id="servantsLabel" alignment="CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="101.0" prefWidth="70.0" text="10" textFill="#bcc600"> 
          <font> 
           <Font name="Trattatello" size="24.0" /> 
          </font> 
         </Label> 
        </children> 
        </Pane> 
        <Pane prefHeight="85.0" translateX="4.0" GridPane.rowIndex="3"> 
        <children> 
         <ImageView fitHeight="79.0" fitWidth="71.0" layoutX="1.0" layoutY="3.0" pickOnBounds="true" preserveRatio="true"> 
          <image> 
           <Image url="@../../../../resources/images/Emporiums.png" /> 
          </image> 
         </ImageView> 
         <Label fx:id="emporiumsLabel" alignment="CENTER" contentDisplay="CENTER" layoutY="8.0" prefHeight="73.0" prefWidth="71.0" text="10"> 
          <font> 
           <Font name="Trattatello" size="36.0" /> 
          </font> 
         </Label> 
        </children> 
        </Pane> 
       </children> 
      </GridPane> 
     </children> 
     </AnchorPane> 
     <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0"> 
     <children> 
      <SplitPane dividerPositions="0.5345345345345346" orientation="VERTICAL" prefHeight="335.0" styleClass="mainPlayerRootPane" stylesheets="@style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
       <items> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0"> 
        <children> 
         <SplitPane dividerPositions="0.5" prefHeight="174.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
          <items> 
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0"> 
           <children> 
            <ListView fx:id="cardsListView" editable="true" orientation="HORIZONTAL" prefHeight="172.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
           </children> 
           </AnchorPane> 
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0"> 
           <children> 
            <ListView fx:id="permitsListView" orientation="HORIZONTAL" prefHeight="172.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
           </children> 
           </AnchorPane> 
          </items> 
         </SplitPane> 
        </children> 
        </AnchorPane> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="134.0" prefWidth="710.0" /> 
       </items> 
      </SplitPane> 
     </children> 
     </AnchorPane> 
    </items> 
</SplitPane> 

它的控制器类:

package client.view.GUI; 


import java.util.List; 
import client.model_properties.PlayerProperty; 
import javafx.collections.ListChangeListener; 
import javafx.fxml.FXML; 
import javafx.scene.Node; 
import javafx.scene.control.Label; 
import javafx.scene.control.ListView; 
import javafx.scene.control.SplitPane; 

public class MainPlayerPaneController { 

    @FXML 
    private Label victoryPointsLabel; 

    @FXML 
    private Label coinsLabel; 

    @FXML 
    private Label servantsLabel; 

    @FXML 
    private Label emporiumsLabel; 

    @FXML 
    private SplitPane mainPlayerRootPane; 

    @FXML 
    private ListView<PoliticalCardView> cardsListView; 

    @FXML 
    private ListView<PoliticalCardView> permitsListView; 

    @FXML 
    private void initialize(){ 

    } 

    public void initializeMainPlayerPane(PlayerProperty mainPlayer) { 

     this.victoryPointsLabel.textProperty().bind(mainPlayer.getVictoryPoint().asString()); 
     this.coinsLabel.textProperty().bind(mainPlayer.getCoins().asString()); 
     this.servantsLabel.textProperty().bind(mainPlayer.getnOfServants().asString()); 
     this.emporiumsLabel.textProperty().bind(mainPlayer.getAvailableEmporiums().asString()); 

     this.addToDeckView(mainPlayer.getDeck()); 
     mainPlayer.getDeckProperty().addListener(new DeckListChangeListener()); 

    } 

    private void addToDeckView(List<? extends String> cards) { 
     PoliticalCardView auxCard; 
     for(String n : cards) { 
      auxCard = new PoliticalCardView(n); 
      auxCard.initializeCardView(); 
      this.cardsListView.getItems().add(auxCard); 
     } 
    } 

    private class DeckListChangeListener implements ListChangeListener<String> { 

     @Override 
     public void onChanged(javafx.collections.ListChangeListener.Change<? extends String> c) { 

      List<? extends String> added = c.getAddedSubList(); 
      List<? extends String> removed = c.getRemoved(); 

      for(String n : removed) { 
       Node toRemove = cardsListView.getItems().stream() 
       .filter(r -> ((PoliticalCardView) r).getColor().equals(n)) 
       .findFirst() 
       .get(); 

       cardsListView.getItems().remove(toRemove); 
      } 

      addToDeckView(added); 
     } 



    } 
} 

,我开了代表证类。

package client.view.GUI; 

import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import utility.UserInterfaceUtilities; 

public class PoliticalCardView extends ImageView { 

    private Image cardImage; 
    private String color; 

    public PoliticalCardView(String color) { 
     this.color = color; 
    } 

    public void initializeCardView() { 
     this.cardImage = UserInterfaceUtilities.POLITICAL_COLORS_TO_IMAGES.get(this.color); 
     this.setImage(cardImage); 
     this.setFitHeight(150); 
     this.setFitWidth(100); 
    } 

    public String getColor() { 
     return this.color; 
    } 

} 

我想知道在哪里的研究2天的问题,因为我还没有发现任何在互联网上类似,也是我用它工作得很好,其他的滚动条。

回答

0

的问题是在这里:

<SplitPane fx:id="mainPlayerRootPane" disable="true" ..... > 

SplitPane被禁用。

当您通过设置其disabledProperty来禁用节点时,其所有子节点都将被禁用。

指示此节点是否被禁用。如果在场景图中的某个节点或其某个 祖先上将disable设置为true,节点将变为 。

从FXML声明删除disable="true"避免禁用您ListViewScrollPane