繁体   English   中英

ListView或ScrollPane滚动条不滚动

[英]ListView or ScrollPane scrollbars don't scroll

对于大学项目,我必须为桌面游戏的在线版本创建GUI,玩家可以通过扩展JavaFX ImageView创建一些卡片,我想在运行时将它们放在界面中。

因此我在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;
    }

}

我想知道问题在哪里,因为在两天的研究中我没有在互联网上发现任何类似的东西,我也使用其他工作正常的滚动条。

问题出在这里:

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

您的SplitPane已禁用。

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

指示是否禁用此节点。 如果在场景图中的自身或其祖先之一上将disable设置为true,则节点将被禁用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM