JSplitPane有一个拆分器来拆分两个组件。 分割条可以水平或垂直显示。
用户可以向上/向下或向左/向右移动分割条,因此一个组件比另一个组件获得更多的空间。
JSplitPane类提供了许多构造函数。 我们可以使用它的默认构造函数创建它,并使用它的setTopComponent(Component c)添加两个组件,setBottomComponent(Component c),setLeftComponent(Component c),setRightComponent(Component c)。
JSplitPane可以以连续或非连续的方式重绘组件。当我们改变分割条的位置。
通过调用JSplitPane的resetToPreferredSizes()方法将分隔符位置重置到该位置。
使用setDividerLocation(newLocation)更改dividerLocation属性。0.0和1.0,表示JSplitPane容器宽度的百分比。
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JSplitPane; public class Main { public static void main(String[] a) { JFrame horizontalFrame = new JFrame(); horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent topButton = new JButton("Left"); JComponent bottomButton = new JButton("Right"); final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(topButton); splitPane.setBottomComponent(bottomButton); horizontalFrame.add(splitPane, BorderLayout.CENTER); horizontalFrame.setSize(150, 150); horizontalFrame.setVisible(true); splitPane.setDividerLocation(0.5); } }
设置方向:JSplitPane.VERTICAL_SPLIT或JSplitPane.HORIZONTAL_SPLIT
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JSplitPane; public class Main { public static void main(String[] a) { JFrame horizontalFrame = new JFrame(); horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent leftButton = new JButton("Left"); JComponent rightButton = new JButton("Right"); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setLeftComponent(leftButton); splitPane.setRightComponent(rightButton); horizontalFrame.add(splitPane, BorderLayout.CENTER); horizontalFrame.setSize(150, 150); horizontalFrame.setVisible(true); } }
调整组件大小和使用单触式可扩展分隔线
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JSplitPane; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Property Split"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setContinuousLayout(true); splitPane.setOneTouchExpandable(true); JComponent topComponent = new JButton("A"); splitPane.setTopComponent(topComponent); JComponent bottomComponent = new JButton("B"); splitPane.setBottomComponent(bottomComponent); frame.add(splitPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } }
import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; // w w w . j a v a2 s.c o m public class Main{ public static void main(String[] a) { int HORIZSPLIT = JSplitPane.HORIZONTAL_SPLIT; int VERTSPLIT = JSplitPane.VERTICAL_SPLIT; boolean continuousLayout = true; JLabel label1 = new JLabel("a"); JLabel label2 = new JLabel("b"); JLabel label3 = new JLabel("c"); JSplitPane splitPane1 = new JSplitPane(VERTSPLIT, continuousLayout, label1, label2); splitPane1.setOneTouchExpandable(true); splitPane1.setDividerSize(2); splitPane1.setDividerLocation(0.5); JSplitPane splitPane2 = new JSplitPane(HORIZSPLIT, splitPane1, label3); splitPane2.setOneTouchExpandable(true); splitPane2.setDividerLocation(0.4); splitPane2.setDividerSize(2); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(splitPane2); frame.pack(); frame.setVisible(true); } }
import java.awt.BorderLayout; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; // w w w . j a va2s . co m import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JSplitPane; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Property Split"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setContinuousLayout(true); splitPane.setOneTouchExpandable(true); JComponent topComponent = new JButton("A"); splitPane.setTopComponent(topComponent); JComponent bottomComponent = new JButton("B"); splitPane.setBottomComponent(bottomComponent); PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent changeEvent) { JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource(); String propertyName = changeEvent.getPropertyName(); if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) { int current = sourceSplitPane.getDividerLocation(); System.out.println("Current: " + current); Integer last = (Integer) changeEvent.getNewValue(); System.out.println("Last: " + last); Integer priorLast = (Integer) changeEvent.getOldValue(); System.out.println("Prior last: " + priorLast); } } }; splitPane.addPropertyChangeListener(propertyChangeListener); frame.add(splitPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } }
Java Swing教程 -Java Swing JSliderJSlider允许我们通过沿着轨道滑动旋钮从两个整数之间的值列表中选择一个值。JSlider有四个重...
MyBatis-Plus 条件构造器-AbstractWrapperhaving(String sqlHaving, Object... params)having(boolean condition...
安装要使用MyBatis,只需将mybatis-x.x.x.jar文件置于类路径(classpath)中即可。如果使用Maven来构建项目,则需将下面的依...
映射器(mappers)既然MyBatis的行为已经由上述元素配置完了,我们现在就要来定义SQL映射语句了。但首先,我们需要告诉MyBatis到...
春天教程 - 春天松散耦合例子...项目代码我们将创建一个有几个类的项目。项目将以不同的格式输出数据。我们可以选择以CSV格式或J...
Spring教程 - 弹簧属性我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。以下部分显示如何将数据填充到java.uti...
Spring教程 -Spring表达式语言运算符Spring Expression Language支持标准的数学,逻辑或关系运算符。Spring Expression Language...
MVC 框架教程Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活、松散耦合的 web 应用程序的组件。MVC 模式...