sample4_17.java

来自「java的swt图形程序」· Java 代码 · 共 82 行

JAVA
82
字号
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class Sample4_17 {
  private static Label label;
  public static void main(String[] args) {
    FormData data;
    GridData gridData;
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("FormLayout示例");
    FormLayout formlayout = new FormLayout(); //创建表格布局对象
    shell.setLayout(formlayout);
    label=new Label(shell,SWT.BORDER);  //在shell中创建Label对象
    label.setText("Label One");
    final FormData fd_label = new FormData();
    fd_label.top = new FormAttachment(0, 5);  //Label与shell上边框相距5象素
    fd_label.left = new FormAttachment(0, 5); // Label与shell左边框相距5象素
    fd_label.bottom = new FormAttachment(50, -5);//Label在shell水平中线上方5象素
    fd_label.right = new FormAttachment(50, -5); // Label在shell垂直中线左边5象素
    label.setLayoutData(fd_label);
    Composite composite = new Composite(shell, SWT.NONE);//创建面板
    GridLayout gridLayout = new GridLayout();  //创建网格布局对象
    gridLayout.marginHeight = 0;  
    gridLayout.marginWidth = 0;
    composite.setLayout(gridLayout);    //设置面板布局方式为网格布局
    final Button b1 = new Button(composite, SWT.PUSH); //在composite上创建button B1
    b1.addSelectionListener(new SelectionAdapter() {
    	public void widgetSelected(SelectionEvent arg0) {
    		b1.setText("3232");
    	}
    });
    b1.addMouseListener(new MouseAdapter() {
    	public void mouseDoubleClick(MouseEvent arg0) {
    		b1.setVisible(false);
    	}
    });
     
    b1.setText("B1");
    final GridData gd_b1 = new GridData(GridData.FILL_BOTH);
    b1.setLayoutData(gd_b1);  
    Button b2 = new Button(composite, SWT.PUSH);  //B2设置同B1
    b2.setText("B2");
    gridData = new GridData(GridData.FILL_BOTH);
    b2.setLayoutData(gridData);
    Button b3 = new Button(composite, SWT.PUSH); //B2设置同B1
    b3.addSelectionListener(new SelectionAdapter() {
    	public void widgetSelected(SelectionEvent arg0) {
    	}
    });
    b3.setText("B3");
    gridData = new GridData(GridData.FILL_BOTH);
    b3.setLayoutData(gridData);
    data = new FormData();    //创建FormData对象
    data.top = new FormAttachment(0, 5);    //设置composite距shell上边框5象素
    data.left = new FormAttachment(label, 5);//设置composite距label 5象素
    data.bottom = new FormAttachment(50, -5);//设置composite在shell水平中线上方5象素
    data.right = new FormAttachment(100, -5);//设置composite在shell右边框的左侧5象素
    composite.setLayoutData(data);          //设置composite的布局数据
    Text text=new Text(shell,SWT.BORDER);  //创建Text对象
    text.setText("Text");
    data = new FormData();    //创建表格布局数据
    data.top = new FormAttachment(label, 5);  //text上方离label 5象素
    data.left = new FormAttachment(0, 5);      // text左边离shell左边框5象素
    data.bottom = new FormAttachment(100, -5); // text下边框离shell下边框5象素
    data.right = new FormAttachment(100, -5);  // text右边框离shell右边框5象素
    text.setLayoutData(data);                  //设置text的布局数据
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?