SWT创建一个被解雇的小部件(滚动复合)

时间:2015-04-07 09:12:26

标签: java swt

我希望我能创建一个我以前处理过的复合材料。目的是显示和隐藏循环复合以更改屏幕(滚动复合)。提前谢谢。

public class prog {
    static public void main(String[] args)  {
        final Display display = new Display();
        final Shell shell     = new Shell  (display, SWT.CLOSE | SWT.TITLE | SWT.MIN );
        shell.setText("EXAMPLE");
        shell.setLayout(new GridLayout(1,false));

        //I want comp1 initially be shown and then being hidden and that is shown comp2 with its components

        final Composite comp1 = new Composite(shell, SWT.NONE);
        comp1.setLayout(new GridLayout(1,false));
        comp1.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

        final Composite comp2 = new Composite(shell, SWT.BORDER);
        comp2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

        GridData data = (GridData)comp2.getLayoutData();  // Assumes layout was set earlier
        data.exclude = true;   // Don't include control in the layout

        comp2.setVisible(false);

        comp2.layout(true);

        Label t = new Label(comp2, SWT.NONE);
        t.setText("must be shown");

        Button btn_create = new Button(comp1, SWT.PUSH);
        btn_create.setText("change composite");
        btn_create.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true,1,1));

        btn_create.addSelectionListener(new SelectionAdapter() {        //evento quando il bottone viene premuto
            public void widgetSelected(SelectionEvent e) {
                GridData data3 = (GridData)comp1.getLayoutData();  // Assumes layout was set earlier

                data3.exclude =     true;   // Don't include control in the layout

                comp1.setVisible(false);

                comp1.layout(true);

                GridData data2 = (GridData)comp2.getLayoutData();  // Assumes layout was set earlier

                data2.exclude = false;   // Don't include control in the layout

                comp2.setVisible(true);

                comp2.layout(true);
                System.out.println(comp2.getVisible());



            }});

        //swt_create.create(display, shell, create);    
        shell.setSize(500, 100);

        shell.addControlListener(new ControlAdapter() {     // fa in modo che la dimensione della finestra si sempre
            public void controlResized(ControlEvent e) {    // uguale
                shell.setSize(500,400);
            }
        });  
        shell.open();
        shell.pack();        

        while (!shell.isDisposed()) {
              if (!display.readAndDispatch()) {    // questo etodo serve a leggere gli eventi che compiamo durante 
                        display.sleep();           // l'esecuzione, quando si clicca sulla x si passa avanti e il ciclo viene
               }                                   // chiuso 
            }

        display.dispose();
}}

1 个答案:

答案 0 :(得分:1)

一旦控制器被处理掉,它就会消失,无法重复使用。

您可以通过调用控件上的setVisible(false)来暂时隐藏控件,使其不可见。

当您使控件可见/不可见时,您需要更新父Composite的布局。根据您用于Composite的布局,您可能还需要调整布局。

例如,如果您使用GridLayout作为父组合,则可以使用exclude的{​​{1}}选项省略控件:

GridData

要显示控件反转设置:

GridData data = (GridData)control.getLayoutData();  // Assumes layout was set earlier

data.exclude = true;   // Don't include control in the layout

control.setVisible(false);

....

parentComposite.layout(true);