The tool for organizing Swt/jface controls: Layout

Source: Internet
Author: User
Tags constructor

In the visual programming age, most visual GUI development tools provide the ability to arrange controls in a form in a certain rule. But for Java, there are not many tools to support visual development, although there are some such tools, but they are mostly third-party products, there is some lack of stability. As a result, when you use Java to write GUI programs, you use a layout (Layout) to control the position of the controls on the form.

This article mainly discusses how to use the layout provided in SWT to arrange the location of the control and illustrate the process through an instance. There are 5 layouts available in SWT: Filllayout, Rowlayout, GridLayout, Formlayout, and Stacklayout. I'll discuss the use of these 5 layouts in more detail below.

Filllayout

Filllayout is the simplest layout. It arranges the controls either horizontally or vertically, and each control has the same width or height. The use of filllayout is generally divided into 2 steps.

1. Create a Filllayout object.

2. Use the SetLayout method to set the layout of the Shell object.

The following code uses Filllayout to put 3 buttons on the shell, as follows:

package layout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class TestFillLayout
{
 public static void main(String[] args)
 {
  Display display = new Display();
  Shell shell = new Shell(display, SWT.DIALOG_TRIM);
  shell.setText("FillLayout演示");
  shell.setSize(400, 300);
  // 设置shell的布局
  FillLayout layout = new FillLayout();
  shell.setLayout(layout);
  // 向shell添加控件
  Button button1 = new Button(shell, SWT.PUSH);
  button1.setText("按钮1");
  Button button2 = new Button(shell, SWT.PUSH);
  button2.setText("按钮2");
  Button button3 = new Button(shell, SWT.PUSH);
  button3.setText("按钮3");
  shell.open();
  while (!shell.isDisposed())
  {
   if (!display.readAndDispatch())
   {
    display.sleep();
   }
  }
  display.dispose();
 }
}

The interface is shown in Figure 1.

Figure 1 The shell interface using horizontal filllayout

If you want the controls on the shell to be arranged vertically, you can set the Type property to Swt.vertical when the layout is established. The code is as follows:

FillLayout layout = new FillLayout();
layout.type = SWT.VERTICAL;
shell.setLayout(layout);

Figure 2 is an effect chart that controls the vertical arrangement

Figure 2 The Shell interface using portrait filllayout

The Filllayout constructor was overloaded 2 times. One of the constructors has one argument, and this argument is type. Therefore, we can also assign values to the type through the Filllayout constructor.

shell.setLayout(new FillLayout(SWT.VERTICAL));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.