简体   繁体   English

如何在Java Swing中创建二维SplitPane

[英]How can I create two dimensional SplitPane in Java Swing

I want to create a two dimensional JSplitpane like design in Java swing. 我想在Java swing中创建一个二维JSplitpane样的设计。 Such that the JFrame will be split into 4 parts, and upper and lower parts are separated by another split, and left and right part are separated by yet another split line. 这样, JFrame将被拆分为4个部分,上部和下部被另一个拆分分隔开,而左部和右部被另一个拆分线分隔开。 Also if I click and drag any part of vertical split line, the complete line should move in dragged direction. 另外,如果我单击并拖动垂直分割线的任何部分,则整行应沿拖动方向移动。

截图

I am trying to achieve this, by using split pane within split pane. 我正在尝试通过在拆分窗格中使用拆分窗格来实现这一点。 But then on dragging vertical split line, it only drags either components below horizontal line or above horizontal split line. 但是随后拖动垂直分割线时,它只会将组件拖动到水平线以下或水平分割线之上。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Demo extends JFrame {

int screenwidth=760,screenheigth=550;

JSplitPane top_sp,bottom_sp,main_sp;

JButton b1,b2,b3,b4,b5,b6;
JButton b7,b8,b9,b10;

MailClient(){
    setSize(screenwidth,screenheigth);
    setLayout(new BorderLayout());
    setTitle("Demo");
    setDefaultCloseOperation(EXIT_ON_CLOSE);    
    setLocationRelativeTo(null);

    b1=new JButton("B1");
    b2=new JButton("B2");
    b3=new JButton("B3");
    b4=new JButton("B4");
    b5=new JButton("B5");
    b6=new JButton("B6");
    b7=new JButton("B7");
    b8=new JButton("B8");
    b9=new JButton("B9");
    b10=new JButton("B10");     

    JPanel topleft=new JPanel(new FlowLayout());
    topleft.add(b1);
    topleft.add(b2);

    JPanel topright=new JPanel(new FlowLayout(FlowLayout.CENTER));
    topright.add(b3);
    topright.add(b4);
    topright.add(b5);
    topright.add(b6);

    JPanel bottomleft=new JPanel(new FlowLayout(FlowLayout.CENTER));

    bottomleft.add(b7);
    bottomleft.add(b8);
    bottomleft.add(b9);
    bottomleft.add(b10);

    JPanel bottomright=new JPanel(new FlowLayout(FlowLayout.CENTER));
    bottomright.add(new JLabel("TABLE"));

    top_sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,topleft,topright);
    bottom_sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,bottomleft,bottomright);

    main_sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,top_sp,bottom_sp);

    add(main_sp,"Center");

    setVisible(true);
}

public static void main(String args[]){
    Demo demo=new Demo();

}

}

You can use a property change listener to detect when the split pane divider has been moved, and then set the location of the other split pane: 您可以使用属性更改侦听器来检测分割窗格分隔符何时被移动,然后设置另一个分割窗格的位置:

import javax.swing.*;
import java.awt.*;

public class Example extends JFrame {

    public Example() {

        JPanel topLeftPanel = new JPanel();
        JPanel topRightPanel = new JPanel();
        JPanel bottomLeftPanel = new JPanel();
        JPanel bottomRightPanel = new JPanel();

        JSplitPane topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, topLeftPanel, topRightPanel);

        JSplitPane bottomPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, bottomLeftPanel, bottomRightPanel);

        topPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, pce -> {
            bottomPane.setDividerLocation((int) pce.getNewValue());
        });

        bottomPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, pce -> {
            topPane.setDividerLocation((int) pce.getNewValue());
        });

        JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPane, bottomPane);

        setContentPane(mainPane);
        setLocationRelativeTo(null);
        setMinimumSize(new Dimension(400, 400));
        setVisible(true);
    }

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

you have to listen to the split-change: 您必须聆听拆分更改:

split.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, 
    new PropertyChangeListener() {
       @Override
       public void propertyChange(PropertyChangeEvent pce) {}
});

(see Detecting JSplitPane Divider Movement for further information) (有关更多信息,请参见检测JSplitPane分频器运动 )。

whenever you change the size of one JSplitPane, you have to change the other as well. 每当更改一个JSplitPane的大小时,也必须更改另一个。

split.setDividerLocation(proportionalLocation);

You can also check out the Split Pane Synchronizer . 您也可以签出“ 拆分窗格同步器”

This is a reusable class that allows you to synchronize 2 (or more) split panes. 这是一个可重用的类,它使您可以同步2个(或更多)拆分窗格。 The classes uses a single PropertyChangeListener to manage the change in divider location so your application code doesn't need to keep track of each split pane separately. 这些类使用单个PropertyChangeListener来管理分隔线位置的更改,因此您的应用程序代码无需分别跟踪每个拆分窗格。

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

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