Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置两个垂直拆分窗格,以便拖动每个窗格不会影响其他窗格?_Java_Javafx_Splitpane - Fatal编程技术网

Java 如何设置两个垂直拆分窗格,以便拖动每个窗格不会影响其他窗格?

Java 如何设置两个垂直拆分窗格,以便拖动每个窗格不会影响其他窗格?,java,javafx,splitpane,Java,Javafx,Splitpane,我正在尝试设置一个包含三个拆分窗格的UI。前两个是垂直窗格,位于屏幕的左侧和右侧。每个拆分的一侧都有一个标题窗格。用户可以从这些窗格中选择要包含在中心窗格字段中的项目。底部还有一个与此问题无关的水平窗格 用户可以通过拖动垂直分隔符或单击相关切换按钮(电影、书籍等)来打开这些侧窗格,以显示该窗格 我遇到的问题是,我想让拖动一个垂直分隔线不会移动另一个。但是,由于我无法找到在不将一个垂直拆分窗格放入另一个垂直窗格的情况下进行设置的方法,因此这总是会导致移动一个分隔符同时移动另一个分隔符的情况。例如,

我正在尝试设置一个包含三个拆分窗格的UI。前两个是垂直窗格,位于屏幕的左侧和右侧。每个拆分的一侧都有一个标题窗格。用户可以从这些窗格中选择要包含在中心窗格字段中的项目。底部还有一个与此问题无关的水平窗格

用户可以通过拖动垂直分隔符或单击相关切换按钮(电影、书籍等)来打开这些侧窗格,以显示该窗格

我遇到的问题是,我想让拖动一个垂直分隔线不会移动另一个。但是,由于我无法找到在不将一个垂直拆分窗格放入另一个垂直窗格的情况下进行设置的方法,因此这总是会导致移动一个分隔符同时移动另一个分隔符的情况。例如,在以下代码的情况下,移动左侧(胶片)分割窗格的垂直分隔符会移动右侧垂直分隔符

有人能帮忙吗

package pane2;

import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.application.*; 
import javafx.beans.property.DoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.*; 
import javafx.scene.*; 
import javafx.scene.layout.*; 
import javafx.scene.control.*; 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.input.*;
import javafx.stage.Stage;

public class Pane2 extends Application {

SplitPane rightSplit;
DoubleProperty rightSplitDividerPos;
TitledPane books;
ToggleButton selectBooks;
VBox booksBox;
VBox centre;

SplitPane leftSplit;
DoubleProperty leftSplitDividerPos;
TitledPane films;
ToggleButton selectFilms;
VBox filmsBox;
VBox centreLeft;

SplitPane mainSplit;
DoubleProperty mainSplitDividerPos;
TitledPane arts;
ToggleButton selectArts;
VBox artsBox;

BorderPane root;


public static void main(String[] args) 
{ 
    launch( args); 
} 

 @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Test");

        //Create right-hand titled pane for the books list and centre it in Vbox
        books = new TitledPane();
        books.setText("Books");
        books.setMinWidth(0);
        booksBox = new VBox(0,books);

        //Create central pane and add toggle buttons to open hidden panes on the
        //left, right, and bottom (films, books, and arts respectively)
        selectBooks = new ToggleButton("Books");
        selectFilms = new ToggleButton("Films");
        selectArts = new ToggleButton("Arts");
        centre = new VBox(100,selectBooks,selectFilms,selectArts);
        centre.setPrefWidth(1300);
        centre.setPrefHeight(750);

        //Create split pane to divide the central pane and books list
        rightSplit = new SplitPane();
        rightSplit.getItems().addAll(centre,booksBox);

        //Create left-hand titled pane for the films list and centre it in VBox
        films = new TitledPane();
        films.setText("Films");
        films.setMinWidth(0);
        filmsBox = new VBox(0,films);

        //Create split pane to divide the films list and the central pane
        leftSplit = new SplitPane();
        leftSplit.getItems().addAll(filmsBox,rightSplit);

        //Create mainSplit pane
        arts = new TitledPane();
        arts.setText("arts");
        arts.setMinHeight(0);
        artsBox = new VBox(0,arts);
        mainSplit = new SplitPane();
        mainSplit.setOrientation(Orientation.VERTICAL);
        mainSplit.getItems().addAll(leftSplit,artsBox);

        root = new BorderPane();        
        root.setCenter(mainSplit);

      //Set divider positions for the three dividers
        rightSplitDividerPos = rightSplit.getDividers().get(0).positionProperty();
        rightSplitDividerPos.set(1.0);
        leftSplitDividerPos = leftSplit.getDividers().get(0).positionProperty();
        leftSplitDividerPos.set(0.0);   
        mainSplitDividerPos = mainSplit.getDividers().get(0).positionProperty();
        mainSplitDividerPos.set(1.0);   

        //Start up scene and stage
        Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setMaximized(true);
            primaryStage.show();

        //Event - if the books toggle button is selected, the left divider will
        //move to the right to show the books selection pane
        selectBooks.setOnAction(event -> {
                if(selectBooks.isSelected()){
                leftSplitDividerPos.set(0.15);
                }
                if(!selectBooks.isSelected()){
                leftSplitDividerPos.set(0.0);

                }else{

                }

        });

      //Event - if the films toggle button is selected, the right divider will
        //move to the left to show the films selection pane
        selectFilms.setOnAction(event -> {
            if(selectFilms.isSelected()){
                rightSplitDividerPos.set(0.8);
            }
            if(!selectFilms.isSelected()){
                rightSplitDividerPos.set(1.0);

            }else{

            }

        });

      //Event - if the arts toggle button is selected, the bottom divider will
        //move up to show the arts selection pane
        selectArts.setOnAction(event -> {
            if(selectArts.isSelected()){
                mainSplitDividerPos.set(0.75);
            }
            if(!selectArts.isSelected()){
                mainSplitDividerPos.set(1.0);

            }else{

            }

        });    

    }

}

您的布局中真的需要3个拆分窗格吗?因为我认为您只需使用一个窗格即可获得几乎相同的结果:

SplitPane split = new SplitPane();
VBox left = new VBox(new Label("left"));
left.setStyle("-fx-background-color: cadetblue");
VBox right = new VBox(new Label("right"));
right.setStyle("-fx-background-color: darkorange");
VBox center = new VBox(new Label("center"));
center.setStyle("-fx-background-color: darkgreen");
split.getItems().addAll(left, center, right);

split.setDividerPosition(0,1/(double)3);
split.setDividerPosition(1,2/(double)3);

Scene scene = new Scene(split, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
以下是您的代码实现示例:

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Test");

    //Create central pane and add toggle buttons to open hidden panes on the
    //left, right, and bottom (films, books, and arts respectively)
    ToggleButton selectBooks = new ToggleButton("Books");
    ToggleButton selectFilms = new ToggleButton("Films");
    ToggleButton selectArts = new ToggleButton("Arts");
    VBox centre = new VBox(100,selectBooks,selectFilms,selectArts);

    //Create left-hand titled pane for the films list and centre it in VBox
    TitledPane films = new TitledPane();
    films.setText("Films");
    VBox filmsBox = new VBox(films);

    //Create right-hand titled pane for the books list and centre it in Vbox
    TitledPane books = new TitledPane();
    books.setText("Books");
    VBox booksBox = new VBox(books);

    //Create mainSplit pane
    TitledPane arts = new TitledPane();
    arts.setText("arts");
    VBox artsBox = new VBox(arts);

    SplitPane mainSplit = new SplitPane();
    mainSplit.getItems().addAll(filmsBox, centre, booksBox);
    mainSplit.setDividerPosition(0,1/(double)12);
    mainSplit.setDividerPosition(1,11/(double)12);

    SplitPane root = new SplitPane();
    root.setOrientation(Orientation.VERTICAL);
    root.getItems().addAll(mainSplit, artsBox);
    root.setDividerPosition(0,0.9);
    root.setPrefWidth(1300);
    root.setPrefHeight(750);
    //Start up scene and stage
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.setMaximized(true);
    primaryStage.show();
}

很抱歉,我没有提到我正在用JavaFX做这个。谢谢,我认为这应该很好!我真的没有正确理解分割线!