如何在 Java 中创建左右分割窗格?

java 8object oriented programmingprogramming

要创建左右分割窗格,让我们创建两个组件并将它们分割成 −

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);
   }
}

这将产生以下输出 −


相关文章