java - 如何在 Java Swing 中创建二维 SplitPane

标签 java swing split jframe splitpane

我想在 Java swing 中创建一个类似二维 JSplitpane 的设计。 这样,JFrame 将被分成 4 部分,上下部分由另一条分割线分隔,左右部分由另一条分割线分隔。另外,如果我单击并拖动垂直分割线的任何部分,则完整的线应沿拖动方向移动。

ScreenShot

我试图通过在分割 Pane 中使用分割 Pane 来实现这一目标。但是,在拖动垂直分割线时,它只会拖动水平线下方或水平分割线上方的组件。

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();

}

}

最佳答案

您可以使用属性更改监听器来检测分割 Pane 分隔线何时被移动,然后设置另一个分割 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();
    }
}

关于java - 如何在 Java Swing 中创建二维 SplitPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37654412/

相关文章:

javascript - 拆分文本以每 n 个字符添加一个新行,注意空格

java - Gradle构建在 list 中声明了一个新的Activity,但没有名称,从而产生了一个错误

java - 从 @RestController 返回 Obj

从另一个类调用时,Java 不会改变外观

java - 将 jCombobox 按钮移至左侧

Python 将一个文件拆分为多个文件并添加额外信息

java - 从 BroadcastReceiver 中删除文件

java - Box2D 在碰撞后阻止动态物体移动

java - 启用/禁用 JButton

python - 根据点拆分 Pandas 数据框中的一列