📜  Java Swing-JSeparator

📅  最后修改于: 2020-09-30 06:06:18             🧑  作者: Mango

Java JSeparator

JSeparator类的对象用于提供用于实现分隔线的通用组件。它用于画线以分隔布局中的小部件。它继承了JComponent类。

JSeparator类声明

public class JSeparator extends JComponent implements SwingConstants, Accessible

JSeparator的常用构造函数

Constructor Description
JSeparator() Creates a new horizontal separator.
JSeparator(int orientation) Creates a new separator with the specified horizontal or vertical orientation.

JSeparator常用方法

Method Description
void setOrientation(int orientation) It is used to set the orientation of the separator.
int getOrientation() It is used to return the orientation of the separator.

Java JSeparator示例1

import javax.swing.*;  
class SeparatorExample  
{  
          JMenu menu, submenu;  
          JMenuItem i1, i2, i3, i4, i5;  
          SeparatorExample()  {  
          JFrame f= new JFrame("Separator Example");  
          JMenuBar mb=new JMenuBar();  
          menu=new JMenu("Menu");  
          i1=new JMenuItem("Item 1");  
          i2=new JMenuItem("Item 2");     
          menu.add(i1);
          menu.addSeparator();
          menu.add(i2);
          mb.add(menu);  
          f.setJMenuBar(mb);  
          f.setSize(400,400);  
          f.setLayout(null);  
          f.setVisible(true);  
}  
public static void main(String args[])  
{  
new SeparatorExample();  
}}  

输出:

Java JSeparator示例2

import javax.swing.*;  
import java.awt.*;
public class SeparatorExample  
{  
public static void main(String args[]) {
    JFrame f = new JFrame("Separator Example");   
    f.setLayout(new GridLayout(0, 1));
    JLabel l1 = new JLabel("Above Separator");
    f.add(l1);
    JSeparator sep = new JSeparator();
    f.add(sep);
    JLabel l2 = new JLabel("Below Separator");
    f.add(l2);
    f.setSize(400, 100);
    f.setVisible(true);
  }
}  

输出: