如何在 Java 中建立左右拆分窗格?
要建立左右拆分窗格,我們將建立兩個元件並拆分它們 −
JComponent one = new JLabel("Left Split");
one.setBorder(BorderFactory.createLineBorder(Color.MAGENTA));
JComponent two = new JLabel("Right Split");
two.setBorder(BorderFactory.createLineBorder(Color.ORANGE));現在,我們將拆分它們。這兩個元件將使用 HORIZONTAL_PANE 常量拆分,一個在另一個的左邊 −
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, one, two);
以下是一個在 Java 中建立左右拆分窗格的示例 −
示例
package my;
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("SplitPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent one = new JLabel("Left Split");
one.setBorder(BorderFactory.createLineBorder(Color.MAGENTA));
JComponent two = new JLabel("Right Split");
two.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, one, two);
frame.add(splitPane);
frame.setSize(550, 250);
frame.setVisible(true);
}
}這會產生以下輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP