如何在 Java 中创建 JSplitPane 来划分组件?
首先,设置我们要划分的两个组件 −
JComponent one = new JLabel("Label One"); one.setBorder(BorderFactory.createLineBorder(Color.yellow)); JComponent two = new JLabel("Label Two"); two.setBorder(BorderFactory.createLineBorder(Color.orange));
现在,使用 SplitPane 分割它 −
示例
package my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; public class SwingDemo { public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent one = new JLabel("Label One"); one.setBorder(BorderFactory.createLineBorder(Color.yellow)); JComponent two = new JLabel("Label Two"); two.setBorder(BorderFactory.createLineBorder(Color.orange)); JComponent three = new JLabel("Label Three"); three.setBorder(BorderFactory.createLineBorder(Color.blue)); JComponent four = new JLabel("Label Four"); four.setBorder(BorderFactory.createLineBorder(Color.green)); JSplitPane split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); split1.setLeftComponent(one); split1.setRightComponent(two); JSplitPane split2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); split2.setLeftComponent(three); split2.setRightComponent(four); frame.add(split1, BorderLayout.NORTH); frame.add(split2, BorderLayout.SOUTH); frame.setSize(550, 250); frame.setVisible(true); } }
输出
广告