openplanning

Hướng dẫn và ví dụ Java SWT RowLayout

  1. SWT RowLayout
  2. Các thuộc tính của RowLayout
  3. Ví dụ với RowLayout

1. SWT RowLayout

RowLayout xác định kích thước và vị trí của các thành phần con của một Composite bằng cách đặt chúng hoặc trong các hàng ngang hoặc thẳng đứng trong một Composite cha.
RowLayout nằm ngang:
RowLayout nằm ngang cho phép đặt các thành phần con trên một dòng, và nó có thể đẩy (wrap) các thành phần con xuống dòng dưới nếu dòng hiện tại không có đủ chỗ trống.
// Tạo một RowLayout nằm ngang

RowLayout rowLayout = new RowLayout();

// Hoặc
RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
RowLayout thẳng đứng
RowLayout thẳng đứng xắp xếp các thành phần con trên một cột, và nó sẽ đẩy các thành phần con sang cột tiếp theo nếu cột hiện tại không còn chỗ trống.
// Tạo một RowLayout thẳng đứng:

RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
Ngoài ra, chiều cao và chiều rộng của từng control trong một RowLayout có thể được xác định bằng cách sét đặt một đối tượng RowData vào control sử dụng phương thức setLayoutData().
RowData rowData= new RowData();

rowData.height=80;
rowData.width=110;

button2.setLayoutData(rowData);

2. Các thuộc tính của RowLayout

RowLayout rowLayout = new RowLayout();

rowLayout.wrap = false;
rowLayout.pack = false;
rowLayout.justify = true;
rowLayout.type = SWT.VERTICAL;
rowLayout.marginLeft = 5;
rowLayout.marginTop = 5;
rowLayout.marginRight = 5;
rowLayout.marginBottom = 5;
rowLayout.spacing = 0;

parent.setLayout(rowLayout);
Thuộc tính wrap của RowLayout chỉ định có hoặc không các control sẽ bị đẩy xuống dòng dưới nếu dòng hiện tại không còn đủ không gian.
  • rowLayout.wrap = false;
  • rowLayout.justify = true;
  • margin & spacing

3. Ví dụ với RowLayout

HorizontalRowLayoutDemo.java
package org.o7planning.swt.rowlayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HorizontalRowLayoutDemo {

   public static void main(String[] args) {

       Display display = new Display();
       Shell shell = new Shell(display);
       shell.setText("SWT RowLayout (o7planning.org)");
 
       // Tạo một RowLayout nằm ngang
       RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
       rowLayout.marginLeft = 10;
       rowLayout.marginTop = 15;
       rowLayout.marginRight = 15;
       rowLayout.marginBottom = 25;
       rowLayout.spacing = 5;
     
       shell.setLayout(rowLayout);

       // Button 1
       Button button1 = new Button(shell, SWT.NONE);
       button1.setText("Button 1");

       // Button 2
       Button button2 = new Button(shell, SWT.NONE);
       button2.setText("Button 2");

       RowData rowData = new RowData();
       rowData.height = 70;
       rowData.width = 90;
       button2.setLayoutData(rowData);

       // Button 3
       Button button3 = new Button(shell, SWT.NONE);
       button3.setText("Button 3");

       // Button 4
       Button button4 = new Button(shell, SWT.NONE);
       button4.setText("Button 4");

       // Button 5
       Button button5 = new Button(shell, SWT.NONE);
       button5.setText("Button 5");

       shell.setSize(400, 250);
       shell.open();
       while (!shell.isDisposed()) {
           if (!display.readAndDispatch())
               display.sleep();
       }
       display.dispose();
   }

}
Chạy ví dụ: