Layout SWT:网格布局柱跨度

Layout SWT:网格布局柱跨度,layout,swt,html,composite,grid-layout,Layout,Swt,Html,Composite,Grid Layout,我已经使用SWT好几年了,但我似乎无法理解这一点: 我需要将视图分成两个“区域”:左侧和右侧 因此,我使用了一个具有两列GridLayout的组合来完成这项工作 在右侧组合中,我有固定数量的列(在另一个GridLayout组合中创建),但在左侧组合中,我需要创建跨越组合限制的动态列数… 有没有办法做到这一点 我尝试在左组合中使用RowLayout,但这两个不匹配:-\ 谢谢,因为不清楚你的意思 跨越复合限制的列 我尝试在左组合中使用行布局,但这两个不匹配 因此,我假设您想要为您的左组合动态更改g

我已经使用SWT好几年了,但我似乎无法理解这一点:

我需要将视图分成两个“区域”:左侧和右侧 因此,我使用了一个具有两列GridLayout的组合来完成这项工作

在右侧组合中,我有固定数量的列(在另一个GridLayout组合中创建),但在左侧组合中,我需要创建跨越组合限制的动态列数…

有没有办法做到这一点

我尝试在左组合中使用RowLayout,但这两个不匹配:-\


谢谢,因为不清楚你的意思

  • 跨越复合限制的列
  • 我尝试在左组合中使用行布局,但这两个不匹配
  • 因此,我假设您想要为您的左组合动态更改gridlayout。请参阅下面的代码,特别是
    按钮。addSelectionListener()

    import java.util.Random;
    导入org.eclipse.swt.swt;
    导入org.eclipse.swt.events.SelectionAdapter;
    导入org.eclipse.swt.events.SelectionEvent;
    导入org.eclipse.swt.layout.GridData;
    导入org.eclipse.swt.layout.GridLayout;
    导入org.eclipse.swt.widgets.Button;
    导入org.eclipse.swt.widgets.Composite;
    导入org.eclipse.swt.widgets.Display;
    导入org.eclipse.swt.widgets.Label;
    导入org.eclipse.swt.widgets.Shell;
    公共类DynamicComposite
    {
    公共静态void main(字符串[]args){
    新建DynamicComposite().start();
    }
    私人复合复合材料;
    专用复合排字灯;
    私有随机兰德;
    公开作废开始()
    {
    rand=新随机(System.currentTimeMillis());
    显示=新显示();
    外壳=新外壳(显示);
    setLayout(新的GridLayout(2,true));
    shell.setText(“动态列”);
    createRightComposite(shell);
    createLeftComposite(shell);
    createButtonComposite(外壳);
    shell.open();
    而(!shell.isDisposed()){
    如果(!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    }
    私有void createButtonComposite(Shell)
    {
    标签l=新标签(外壳、SWT分离器| SWT水平);
    GridData GridData=新的GridData(SWT.FILL、SWT.FILL、true、false);
    gridData.horizontalSpan=2;
    l、 setLayoutData(网格数据);
    按钮=新按钮(外壳,SWT.PUSH);
    gridData=新的gridData(SWT.CENTER、SWT.CENTER、true、false);
    gridData.horizontalSpan=2;
    按钮。setLayoutData(gridData);
    setText(“更改列!!”);
    addSelectionListener(新建SelectionAdapter(){
    公共无效WidgeSelected(SelectionEvent e){
    if(compositeLeft==null | | compositeLeft.isDisposed())
    返回;
    GridLayout=(GridLayout)compositeLeft.getLayout();
    int col=兰特nextInt(7);
    如果(列==0)
    col=1;
    layout.numColumns=col;
    //compositeLeft.setLayout(布局);
    compositeLeft.layout();//您需要重新布局组合
    }
    });
    }
    私有void createRightComposite(Shell)
    {
    compositeLeft=新复合材料(外壳,SWT.NONE);
    GridLayout GridLayout=新的GridLayout(3,true);
    gridLayout.marginWidth=0;
    gridLayout.marginHeight=0;
    gridLayout.marginLeft=0;
    gridLayout.marginRight=0;
    gridLayout.marginTop=0;
    gridLayout.marginBottom=0;
    gridLayout.horizontalSpacing=0;
    gridLayout.verticalSpacing=0;
    gridLayout.horizontalSpacing=0;
    compositeLeft.setLayout(网格布局);
    GridData GridData=新的GridData(SWT.FILL,SWT.FILL,true,true);
    compositeLeft.setLayoutData(gridData);
    int计数器=17;
    
    for(int i=0;合成上的iA listener与一些数学一起完成了这个技巧!谢谢!谢谢你的帮助。GridData完成了这个技巧。除了不同宽度的列之外,我如何实现相同的功能?
    import java.util.Random;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    
    public class DynamicComposite 
    {
        public static void main(String[] args) {
            new DynamicComposite().start();
        }
    
        private Composite compositeLeft;
        private Composite compositeRight;
        private Random rand;
    
        public void start()
        {
            rand = new Random(System.currentTimeMillis()); 
    
            Display display = new Display();
            Shell shell = new Shell(display);
            shell.setLayout(new GridLayout(2, true));
            shell.setText("Dynamic Columns");
    
            createRightComposite(shell);
            createLeftComposite(shell);
            createButtonComposite(shell);
    
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
    
        private void createButtonComposite(Shell shell) 
        {
            Label l = new Label(shell, SWT.SEPARATOR|SWT.HORIZONTAL);
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
            gridData.horizontalSpan = 2;
            l.setLayoutData(gridData);
    
            Button button = new Button(shell, SWT.PUSH);
            gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
            gridData.horizontalSpan = 2;
            button.setLayoutData(gridData);
            button.setText("Change Columns !!");
    
            button.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    if(compositeLeft == null || compositeLeft.isDisposed())
                        return;
    
                    GridLayout layout = (GridLayout)compositeLeft.getLayout();
                    int col = rand.nextInt(7);
                    if(col == 0)
                        col = 1;
                    layout.numColumns = col;
                    //compositeLeft.setLayout(layout);
                    compositeLeft.layout(); // You need to re-layout your composite
                }
            });
        }
    
        private void createRightComposite(Shell shell) 
        {
            compositeLeft = new Composite(shell, SWT.NONE);
            GridLayout gridLayout = new GridLayout(3, true);
            gridLayout.marginWidth = 0;
            gridLayout.marginHeight = 0;
            gridLayout.marginLeft = 0;
            gridLayout.marginRight= 0;
            gridLayout.marginTop = 0;
            gridLayout.marginBottom = 0;
            gridLayout.horizontalSpacing = 0;
    
            gridLayout.verticalSpacing = 0;
            gridLayout.horizontalSpacing = 0;
            compositeLeft.setLayout(gridLayout);
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            compositeLeft.setLayoutData(gridData);
    
            int counter = 17;
            for(int i=0; i<counter; i++)
            {
                Button button = new Button(compositeLeft, SWT.PUSH);
                button.setText("Button " + (i+1));
                GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
                button.setLayoutData(bData);
            }
        }
    
        private void createLeftComposite(Shell shell) 
        {
            compositeRight = new Composite(shell, SWT.NONE);
            GridLayout gridLayout = new GridLayout(3, true);
            gridLayout.marginWidth = 0;
            gridLayout.marginHeight = 0;
            gridLayout.marginLeft = 0;
            gridLayout.marginRight= 0;
            gridLayout.marginTop = 0;
            gridLayout.marginBottom = 0;
            gridLayout.horizontalSpacing = 0;
            compositeRight.setLayout(gridLayout);
    
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            compositeRight.setLayoutData(gridData);
    
            int counter = 7;
            for(int i=0; i<counter; i++)
            {
                Button button = new Button(compositeRight, SWT.PUSH);
                button.setText("Button " + (i+1));
                GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
                button.setLayoutData(bData);
            }
        }
    }