`

重写JSplitPane的BasicSplitPaneUI

阅读更多
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;

import sun.swing.DefaultLookup;

public class MySplitPaneUI extends BasicSplitPaneUI {

	private static final Color BG_COLOR = Color.BLACK;
	public JButton theL$UButton, theR$DButton;

	public MySplitPaneUI() {
		super();
	}

	public static ComponentUI createUI(JComponent c) {
		return new MySplitPaneUI();
	}

	@Override
	public void paint(Graphics g, JComponent c) {
		super.paint(g, c);
		g.setColor(Color.BLACK);
		int width = c.getWidth();
		int height = c.getHeight();
		g.drawRect(3, 3, width - 6, height - 6);
	}

	/**
	 * Creates the default divider.
	 */
	@Override
	public BasicSplitPaneDivider createDefaultDivider() {
		return new MyBasicSplitPaneDivider(this);
	}

	private class MyBasicSplitPaneDivider extends BasicSplitPaneDivider {

		private static final long serialVersionUID = 1L;
		private int oneTouchSize, oneTouchOffset;
		boolean centerOneTouchButtons;
		/* Center空白区域 */
		private int area_x, area_y;

		@SuppressWarnings("unchecked")
		public MyBasicSplitPaneDivider(MySplitPaneUI ui) {
			super(ui);
			oneTouchSize = DefaultLookup.getInt(ui.getSplitPane(), ui, "SplitPane.oneTouchButtonSize", ONE_TOUCH_SIZE);
			oneTouchOffset = DefaultLookup.getInt(ui.getSplitPane(), ui, "SplitPane.oneTouchButtonOffset", ONE_TOUCH_OFFSET);
			centerOneTouchButtons = DefaultLookup.getBoolean(ui.getSplitPane(), ui, "SplitPane.centerOneTouchButtons", true);
			//oneTouchSize = ONE_TOUCH_SIZE;
			//oneTouchOffset = ONE_TOUCH_OFFSET;
			//centerOneTouchButtons = true;
			setLayout(new DividerLayout());
			setBasicSplitPaneUI(ui);
			orientation = splitPane.getOrientation();
			setCursor((orientation == JSplitPane.HORIZONTAL_SPLIT) ? Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR) : Cursor
					.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
			setBackground(UIManager.getColor("SplitPane.background"));
		}

		/**
		 * Creates and return an instance of JButton that can be used to collapse the right component in the split pane.
		 */
		@SuppressWarnings("serial")
		@Override
		protected JButton createRightOneTouchButton() {
			JButton b = new JButton() {

				public void setBorder(Border border) {
				}

				@Override
				public void paint(Graphics g) {
					if (splitPane != null) {
						int[] xs = new int[3];
						int[] ys = new int[3];
						int blockSize;

						// Fill the background first ...   
						g.setColor(Color.WHITE);
						g.fillRect(0, 0, this.getWidth(), this.getHeight());

						// ... then draw the arrow.   
						if (orientation == JSplitPane.VERTICAL_SPLIT) {
							blockSize = Math.min(getHeight(), oneTouchSize);
							xs[0] = blockSize;
							xs[1] = blockSize << 1;
							xs[2] = 0;
							ys[0] = blockSize;
							ys[1] = ys[2] = 0;
						} else {
							blockSize = Math.min(getWidth(), oneTouchSize);
							xs[0] = xs[2] = 0;
							xs[1] = blockSize;
							ys[0] = 0;
							ys[1] = blockSize;
							ys[2] = blockSize << 1;
						}
						g.setColor(BG_COLOR);
						g.fillPolygon(xs, ys, 3);
					}
				}

				// Don't want the button to participate in focus traversable.   

				public boolean isFocusTraversable() {
					return false;
				}
			};
			b.setMinimumSize(new Dimension(oneTouchSize, oneTouchSize));
			b.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			b.setFocusPainted(false);
			b.setBorderPainted(false);
			b.setRequestFocusEnabled(false);
			return b;
		}

		/**
		 * Creates and return an instance of JButton that can be used to collapse the left component in the split pane.
		 */
		protected JButton createLeftOneTouchButton() {
			@SuppressWarnings("serial")
			JButton b = new JButton() {

				public void setBorder(Border b) {
				}

				public void paint(Graphics g) {
					if (splitPane != null) {
						int[] xs = new int[3];
						int[] ys = new int[3];
						int blockSize;

						// Fill the background first ...   
						g.setColor(Color.WHITE);
						g.fillRect(0, 0, this.getWidth(), this.getHeight());

						// ... then draw the arrow.   
						g.setColor(BG_COLOR);
						if (orientation == JSplitPane.VERTICAL_SPLIT) {
							blockSize = Math.min(getHeight(), oneTouchSize);
							xs[0] = blockSize;
							xs[1] = 0;
							xs[2] = blockSize << 1;
							ys[0] = 0;
							ys[1] = ys[2] = blockSize;
							g.drawPolygon(xs, ys, 3); // Little trick to make the   
							// arrows of equal size   
						} else {
							blockSize = Math.min(getWidth(), oneTouchSize);
							xs[0] = xs[2] = blockSize;
							xs[1] = 0;
							ys[0] = 0;
							ys[1] = blockSize;
							ys[2] = blockSize << 1;
						}
						g.fillPolygon(xs, ys, 3);
					}
				}

				// Don't want the button to participate in focus traversable.   

				public boolean isFocusTraversable() {
					return false;
				}
			};
			b.setMinimumSize(new Dimension(oneTouchSize, oneTouchSize));
			b.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			b.setFocusPainted(false);
			b.setBorderPainted(false);
			b.setRequestFocusEnabled(false);
			return b;
		}

		@Override
		public void paint(Graphics g) {
			super.paint(g);
			Dimension size = getSize();
			g.setColor(BG_COLOR);
			g.fillRect(1, 1, size.width - 1, size.height - 1);
			g.setColor(Color.GRAY);
			g.drawRect(0, 0, size.width, size.height);
			g.setColor(Color.WHITE);
			//分为水平时跟垂直时,白色区域   
			if (orientation == JSplitPane.VERTICAL_SPLIT) {
				g.fillRect(area_x, 1, 30, size.height - 1);
			} else {
				g.fillRect(1, area_y, size.width - 1, 30);
			}
			theL$UButton = leftButton;
			theR$DButton = rightButton;
			if (leftButton != null) {
				leftButton.repaint();
			}
			if (rightButton != null) {
				rightButton.repaint();
			}
		}

		/**
		 * Used to layout a <code>BasicSplitPaneDivider</code>. Layout for the divider involves appropriately moving the left/right buttons around.
		 * <p>
		 */
		protected class DividerLayout implements LayoutManager {

			public void layoutContainer(Container c) {
				if (leftButton != null && rightButton != null) {
					if (splitPane.isOneTouchExpandable()) {
						Insets insets = getInsets();

						if (orientation == JSplitPane.VERTICAL_SPLIT) {
							int extraX = (insets != null) ? insets.left : 0;
							int blockSize = getHeight();

							if (insets != null) {
								blockSize -= (insets.top + insets.bottom);
								blockSize = Math.max(blockSize, 0);
							}
							blockSize = Math.min(blockSize, oneTouchSize);

							int y = (c.getSize().height - blockSize) / 2;

							if (!centerOneTouchButtons) {
								y = (insets != null) ? insets.top : 0;
								extraX = 0;
							}
							int width = (int) MyBasicSplitPaneDivider.this.getSize().getWidth();
							area_x = width / 2 - oneTouchSize;
							leftButton.setBounds(extraX - oneTouchOffset + width / 2, y, blockSize * 2, blockSize);
							rightButton.setBounds(extraX - oneTouchOffset + oneTouchSize * 2 + width / 2, y, blockSize * 2, blockSize);
						} else {
							int extraY = (insets != null) ? insets.top : 0;
							int blockSize = getWidth();
							if (insets != null) {
								blockSize -= (insets.left + insets.right);
								blockSize = Math.max(blockSize, 0);
							}
							blockSize = Math.min(blockSize, oneTouchSize);

							int x = (c.getSize().width - blockSize) / 2;

							if (!centerOneTouchButtons) {
								x = (insets != null) ? insets.left : 0;
								extraY = 0;
							}
							int height = (int) MyBasicSplitPaneDivider.this.getSize().getHeight();
							area_y = height / 2 - oneTouchSize;
							leftButton.setBounds(x, extraY - oneTouchOffset + height / 2, blockSize, blockSize * 2);
							rightButton.setBounds(x, extraY - oneTouchOffset + oneTouchSize * 2 + height / 2, blockSize, blockSize * 2);
						}
					} else {
						leftButton.setBounds(-5, -5, 1, 1);
						rightButton.setBounds(-5, -5, 1, 1);
					}
				}
			}

			public Dimension minimumLayoutSize(Container c) {
				// NOTE: This isn't really used, refer to   
				// BasicSplitPaneDivider.getPreferredSize for the reason.   
				// I leave it in hopes of having this used at some point.   
				if (splitPane == null) {
					return new Dimension(0, 0);
				}
				Dimension buttonMinSize = null;

				if (splitPane.isOneTouchExpandable() && leftButton != null) {
					buttonMinSize = leftButton.getMinimumSize();
				}

				Insets insets = getInsets();
				int width = getDividerSize();
				int height = width;

				if (orientation == JSplitPane.VERTICAL_SPLIT) {
					if (buttonMinSize != null) {
						int size = buttonMinSize.height;
						if (insets != null) {
							size += insets.top + insets.bottom;
						}
						height = Math.max(height, size);
					}
					width = 1;
				} else {
					if (buttonMinSize != null) {
						int size = buttonMinSize.width;
						if (insets != null) {
							size += insets.left + insets.right;
						}
						width = Math.max(width, size);
					}
					height = 1;
				}
				return new Dimension(width, height);
			}

			public Dimension preferredLayoutSize(Container c) {
				return minimumLayoutSize(c);
			}

			public void removeLayoutComponent(Component c) {
			}

			public void addLayoutComponent(String string, Component c) {
			}
		} // End of class BasicSplitPaneDivider.DividerLayout   
	}
}

 

分享到:
评论

相关推荐

    JSplitPane

    JSplitPane

    JSplitPane的用法

    ### JSplitPane的用法详解 #### 一、引言 `JSplitPane`是Java Swing框架中的一个组件,主要用于创建可调整大小的面板分割器。它可以将界面分为两个部分,并允许用户通过拖动分割条来调整每个部分的大小。这对于...

    kk.rar_jsplitpane UI_成绩管理系统

    【kk.rar】是一个压缩包文件,其中包含了名为"jsplitpane UI_成绩管理系统"的相关内容。这个系统显然是一个教育或教学环境中使用的应用,用于管理学生的成绩。【jsplitpane】是Java Swing库中的一个组件,它在用户...

    java swing-JSplitPane的使用

    JSplitPane是Swing库提供的一个分割面板组件。它可以在图形界面中创建两个子组件,并允许用户通过拖动分割条来调整子组件的大小。

    flex4 实现分隔栏 类似java里面的JSplitPane的功能

    NULL 博文链接:https://silentwu.iteye.com/blog/1605617

    Java Swing中的工具栏(JToolBar)和分割面版(JSplitPane)组件使用案例

    在Swing中,JToolBar和JSplitPane是常用的两个组件,它们分别用于创建工具栏和分割面板。本文将详细介绍这两个组件的使用,并提供相应的代码示例和功能描述。 首先,JToolBar是Swing中用于创建工具栏的组件。工具栏...

    swing分割窗口控件JSplitPane使用方法详解

    JSplitPane是Java Swing库中用于创建可分割窗口界面的一个控件。通过JSplitPane,我们可以把一个主窗口分割成两个部分,每部分可以分别放置不同的组件,以实现复杂布局的设计。这种控件非常适合于那些需要动态分配...

    Swing拆分窗格控件JSplitPane使用详解

    Swing提供了大量用于创建窗口化应用程序的组件,其中一个非常有用的组件就是JSplitPane,即拆分窗格控件。JSplitPane允许用户通过一个可拖动的分割条来调整两个(或更多)组件之间的空间分配。 JSplitPane的基本...

    SplitPanel的使用

    在Java Swing库中,`JSplitPane`是一个非常重要的组件,它允许用户动态地调整两个区域的大小。这个组件常用于构建用户界面,使用户能够根据自己的需求自定义视图的比例。在本教程中,我们将深入探讨`JSplitPane`的...

    实例22 分割一个面板(java绝对新手实例)

    在Java编程语言中,"分割一个面板"通常指的是使用Swing库中的JSplitPane组件来创建用户界面。JSplitPane允许我们将屏幕空间分为两个或更多个可调整大小的部分,这样用户就可以根据自己的需求动态调整各个区域的大小...

    Jtree

    在IT领域,JTree是一种广泛使用的Java Swing组件,它允许用户以树形结构来展示数据。这个组件在GUI(图形用户界面)应用中特别常见,因为它能清晰地组织层次化的信息,比如文件系统目录、数据库结构或者项目组织。...

    java swing笔记

    JSplitPane jSplitPane1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, jScrollPane1, jScrollPane2); ``` 这里创建了一个垂直分割的`JSplitPane`实例,参数`true`表示是否显示连续的分割条,即使两个组件之...

    Java制作浏览器和源代码查看器.rar

      JSplitPane spane=new JSplitPane(JSplitPane.VERTICAL_SPLIT); //实例化分隔面板   p1.add(new JLabel("地址")); //增加组件到面板上   p1.add(jtfAddress);   spane.add(new JScrollPane(jtpShow),...

    面板的分割java

    本文主要分为以下几个部分:理解JSplitPane、实现面板分割、整合其他组件如JList,以及如何增强应用安全性。 ### 一、理解JSplitPane `JSplitPane` 是Swing中的一个类,用于创建可以动态调整大小的分割窗口。它...

    Java JTree+JTable应用

    这通常通过实现`TreeSelectionListener`接口并重写`valueChanged`方法来完成。 `JTextPane`和`JSplitPane`是额外提及的两个组件。`JTextPane`是Swing提供的一种文本组件,支持格式化文本,如富文本、链接等。在某些...

    swing界面设计之Layou

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel); // 设置初始分割比例 splitPane.setDividerLocation(150); // 将JSplitPane添加到主框架 frame.add(splitPane...

    精美Swing 折叠菜单

    这可能涉及到Swing的paintComponent方法,通过重写此方法来实现自定义的绘制逻辑。 在项目中,"FolderPaneDemo" 类很可能是程序的主入口点,它包含了整个菜单系统的实例化和配置。在Eclipse中,你可以直接导入这个...

    继承自JFrame类的窗口应用程序

    这意味着该类自动获得了创建独立窗口的能力,并且可以重写或扩展父类的方法来定制窗口的行为。 ##### 成员变量声明 ```java JTextArea text; JButton btn2 = new JButton(); JButton[] btn = new JButton[5]; ...

    java分栏技术,一个很实用得小程序

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel); splitPane.setDividerLocation(200); // 设置默认的分栏位置 // 将分栏添加到框架中 frame.add(splitPane); ...

    基于Swing的类似编辑器的界面

    本文将深入探讨基于Swing的"类似编辑器的界面"的设计与实现,以及涉及到的关键技术,包括可关闭的选项卡面板、文件复制功能、JSplitPane和滚动面板。 首先,"可关闭的选项卡面板"是一种常见的用户界面设计,常用于...

Global site tag (gtag.js) - Google Analytics