Java 程序,用于设置方向并将组件沿 y 轴拆分


我们首先创建用于拆分的组件。在此,我们有两个标签 -

JComponent one = new JLabel("Label One");
one.setBorder(BorderFactory.createLineBorder(Color.red));
JComponent two = new JLabel("Label Two");
two.setBorder(BorderFactory.createLineBorder(Color.blue));

现在,使用 HORIZONTAL_SPLIT 设置方向并在 x 轴上拆分 -

JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
split.setTopComponent(one);
split.setBottomComponent(two);

以下是设置方向并在 y 轴上拆分组件的示例 -

示例

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.red));
      JComponent two = new JLabel("Label Two");
      two.setBorder(BorderFactory.createLineBorder(Color.blue));
      JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
      split.setTopComponent(one);
      split.setBottomComponent(two);
      frame.add(split, BorderLayout.NORTH);
      frame.setSize(550, 250);
      frame.setVisible(true);
   }
}

这将产生以下输出 -

更新时间:2019 年 7 月 30 日

95 次浏览

启动您的事业

完成课程取得认证

开始
广告