Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Tầm quan trọng của lớp JSeparator trong Java

JSeparator

  • JSeparator là một đường kẻ ngang hoặc dọc hoặc khoảng trống tách biệt các thành phần.
  • Lớp JSeparator dùng để vẽ đường kẻ tách các thành phần trong Layout.
  • Cách dễ nhất thêm separator vào menu hoặc toolbar là gọi phương thức addSeparator() của các lớp JMenu, JPopupMenu và JToolBar.
  • Các phương thức quan trọng của JSeparator là setOrientation() và getOrientation().

Ví dụ

import java.awt.*;
import javax.swing.*;

public class JSeparatorTest extends JFrame {
    private JLabel label1, label2;

    public JSeparatorTest() {
        setTitle("JSeparator Test");
        setLayout(new GridLayout(0, 1));
        label1 = new JLabel("Above Separator");
        add(label1);
        JSeparator sep = new JSeparator(); // add a separator between two labels.
        add(sep);
        label2 = new JLabel("Below Separator");
        add(label2);
        setSize(375, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String args[]) {
        new JSeparatorTest();
    }
}

Kết quả

Tầm quan trọng của lớp JSeparator trong Java