Eclipse插件开发:SWT布局之RowLayout

    RowLayout(行布局)中的控件们可以设定固定的间距,随着列宽的增加,行内的控件数增加,适合做图片墙。

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    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 RowLayoutExample {
    	public static void main(String[] args) {
    		Button button;
    		Display display = new Display();
    		Shell shell = new Shell(display);
    		shell.setText("RowLayout Example");
    		shell.setBounds(100, 100, 400, 100);
    		RowLayout layout = new RowLayout();
    		layout.marginLeft = 10;
    		layout.marginRight = 10;
    		layout.marginTop = 10;
    		layout.marginBottom = 10;
    		layout.spacing = 10;
    		shell.setLayout(layout);
    		for (int i = 1; i <= 20; i++) {
    			button = new Button(shell, SWT.PUSH);
    			button.setText("B" + i);
    			button.addSelectionListener(new SelectionAdapter() {
    				public void widgetSelected(SelectionEvent event) {
    					System.out.println(((Button) event.widget).getText()
    							+ " was clicked!");
    				}
    			});
    		}
    		shell.open();
    		while (!shell.isDisposed()) {
    			if (!display.readAndDispatch())
    				display.sleep();
    		}
    		display.dispose();
    	}
    }
    效果如下:

    本文为风林火山博客原创,转载请注明出处:www.flcoder.com
    validate