Java Swing advanced components-split panel

Split panel

The split panel is implemented by the JSplitPane class, which is used to split the area where it is located into two parts. It can be determined horizontally or vertically according to the situation. There will be a divider between the two parts. Adjust the position of the divider. , You can customize the relative size of the two parts.

Commonly used construction method of JSplitPane class.
1. JSplitPane(): Create a default split panel. Split horizontally by default.
2. JSplitPane (int newOrientation): Create a split panel specifying the split direction. The static constants available for newOrientation are HORIZONTAL_SPLIT (horizontal split) and VERTICAL_SPLIT (vertical split).
3. JSplitPane(int newOrientation, boolena newContinuousLayout): Create a split panel with a specified split direction, and redraw the split panel in a specified way. Setting newContinuousLayout to true means continuous redrawing while adjusting the position of the divider bar, and setting it to false means redrawing only when the position of the divider bar is adjusted.

Commonly used construction methods in the JSplitPane class.

method Description
setOrientation(int orientation) Set the split direction of the split panel
setDividerLocation(int location) Set the absolute position of the divider, that is, the width on the left or the height above the divider
setDividerLocation(double proportionalLocation) Set the relative position of the divider, that is, the percentage of the size on the left or above the divider to the size of the split panel
setDividerSize(int newSize) Set the width of the divider, the default is 5 pixels
setLeftComponent(Component comp) Add components to the left or above the divider
setTopComponent(Component comp) Add components above or to the left of the divider
setRightComponent(Component comp) Set the component to the right or below the divider
setBottomComponent(Component comp) Set the component below or to the right of the divider
setOneTouchExpandable(boolean newValue) Set whether the split panel provides UI widgets, true to provide, false to not provide, not provided by default
setContinuousLayout(boolean newContinuousLayout) Set the redrawing method when adjusting the position of the divider bar, set to rue means continuous redrawing during the process of adjusting the position of the divider bar, set to false means redraw only when the position of the divider bar is adjusted.

Create a split panel, the code is as follows:

package study.czm;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;

public class Study {
    
    

	public static void main(String[] args) {
    
    

		JFrame jf = new JFrame();
		jf.setTitle("表格组件");
		jf.setSize(500, 400);
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
		Container c = jf.getContentPane();

		JSplitPane hSplitPane = new JSplitPane();// 创建一个水平方向的分割面板
		hSplitPane.setLeftComponent(new JLabel("左边"));// 在面板左侧添加一个组件

		JSplitPane vSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);// 创建一个垂直方向的分割面板
		vSplitPane.setOneTouchExpandable(true);// 提供UI小部件
		hSplitPane.setRightComponent(vSplitPane);// 将垂直的分割面板添加到水平分割面板的右侧
		vSplitPane.setTopComponent(new JLabel("上边"));// 在垂直面板上方添加组件
		vSplitPane.setBottomComponent(new JLabel("下边"));// 在垂直面板上方添加组件
		c.add(hSplitPane);

		jf.setVisible(true);

	}
}

Screenshot of running effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/javanofa/article/details/106018905