JSplitPane
2018-01-09 19:23 更新
Java Swing教程 - JSplitPane
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;
/* w w w.j a v a 2s. c o 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[] 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;
/*from ww w .j a v a 2 s .c o 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[] 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;
/*from w ww.jav a 2 s. c o 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);
frame.add(splitPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
嵌套JSplitPane
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);
}
}
嵌套JSplitPane...
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);
}
}
以上内容是否对您有帮助:

免费 AI IDE


更多建议: