尝试在SWT的选项卡中设置按钮的大小/位置

时间:2012-12-21 23:05:29

标签: swt

这应该很简单,但这是我第一次使用SWT。这是我到目前为止所做的。

public class TabsTest {

private Shell shell;
private CTabFolder folder;

public TabsTest(Display display){

    shell = new Shell(display);
    shell.setText("TabsTest");
    shell.setLayout(new FillLayout());

    CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
    folder.setUnselectedCloseVisible(false);
    folder.setSimple(false);

    initUI(folder);

    shell.pack();
    shell.setBounds(500, 500, 400, 500);
    shell.open ();

    while(!shell.isDisposed()){
        if(!display.readAndDispatch())
            display.sleep();
    }   
}

public void initUI(CTabFolder folder){
    CTabItem NFL = new CTabItem(folder, SWT.NONE);
    NFL.setText("NFL Bets");

    Button okButton = new Button(folder, SWT.PUSH);
    okButton.setText("OK");
    okButton.setSize(10,10);

    NFL.setControl(okButton);

    CTabItem NBA = new CTabItem(folder,SWT.NONE);
    NBA.setText("NBA Bets");

    CTabItem CFB = new CTabItem(folder,SWT.NONE);
    CFB.setText("CFB Bets");        

    folder.setSize(800,500);

}

public static void main (String [] args) {
    Display display = new Display();
    new TabsTest(display);
    display.dispose();
}

}

目前给我的是这个....

picture

如何在右下角设置一个小按钮?或者只是将其缩小并移动到某个地方。

1 个答案:

答案 0 :(得分:3)

由于您使用FillLayout,控件占用整个可用空间。你需要的是一种不同的布局。我建议你阅读this article,这将是一个好的开始。

我通常更喜欢GridLayout,因为它非常易于使用,并且可以满足大多数需求。

已修改:修改您的代码以使用GridLayout

public class TabsTest {

    private Shell shell;
    private CTabFolder folder;

    public TabsTest(Display display) {
        shell = new Shell(display);
        shell.setText("TabsTest");
        shell.setLayout(new GridLayout());

        CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
        folder.setUnselectedCloseVisible(false);
        folder.setSimple(false);
        folder.setLayoutData(new GridData(GridData.FILL_BOTH));

        initUI(folder);

        shell.pack();
        shell.setBounds(500, 500, 400, 500);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

    public void initUI(CTabFolder folder) {
        CTabItem NFL = new CTabItem(folder, SWT.NONE);
        NFL.setText("NFL Bets");

        Composite nflParent = new Composite(folder, SWT.NONE);
        nflParent.setBackground(folder.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        nflParent.setLayout(new GridLayout());

        Button okButton = new Button(nflParent, SWT.PUSH);
        okButton.setText("OK");

        GridData gd = new GridData();
        gd.verticalAlignment = GridData.END;
        gd.horizontalAlignment = GridData.END;
        gd.grabExcessHorizontalSpace = true;
        gd.grabExcessVerticalSpace = true;
        okButton.setLayoutData(gd);

        NFL.setControl(nflParent);

        CTabItem NBA = new CTabItem(folder, SWT.NONE);
        NBA.setText("NBA Bets");

        CTabItem CFB = new CTabItem(folder, SWT.NONE);
        CFB.setText("CFB Bets");

        folder.setSize(800, 500);
    }

    public static void main(String[] args) {
        Display display = new Display();
        new TabsTest(display);
        display.dispose();
    }
}