如何在不被覆盖的情况下控制此shell的大小?

时间:2015-07-14 19:54:57

标签: java shell swt

我需要一些SWT帮助。

我用多个get文本标签构建了这个shell。标签正在控制外壳的大小,但我希望外壳为250x200像素。在打开shell之前我已经设置了大小。如何防止它被覆盖?我希望最终能够放入1000行并且能够向下滚动,而不是拥有一个巨大的窗口我无法做任何事情。

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Color;

public class JavaLabelTable {

private Label status;
private Display display;
private Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.CENTER | SWT.V_SCROLL);

public JavaLabelTable(Display display)
{
    initUT(display);
}

public void initUT(Display display)
{
    int q = 110;
    int w = 40;
    int e = q - w;
    int i;
    int y = 120;
    int b = 1;
    int c = 1;
    shell.setLayout(new GridLayout(3, false));
    Label RegisterLabels[] = new Label[y];
    status = new Label(shell, SWT.BORDER); 
    GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);   
    status.setLayoutData(gd2);
    gd2.widthHint = q;
    gd2.heightHint = 15;
    gd2.horizontalSpan = 3;

    for(i=1; i<y; i++)
    {
        if(i % 3 == 1)
        {
            GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
            RegisterLabels[i] = new Label(shell, SWT.NONE);
            RegisterLabels[i].setLayoutData(gd1);
            RegisterLabels[i].setAlignment(SWT.CENTER);
            gd1.widthHint = w;
            gd1.horizontalSpan = 1;
            RegisterLabels[i].setText(Integer.toString(b));
            b++;
        }
        else
        {
            if(i % 3 == 2)
            {
                GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
                RegisterLabels[c] = new Label(shell, SWT.BORDER);
                RegisterLabels[c].setLayoutData(gd1);
                RegisterLabels[c].setAlignment(SWT.CENTER);
                Color col1 = new Color(display, 255, 255, 255);
                RegisterLabels[c].setBackground(col1);
                gd1.widthHint = e;
                gd1.horizontalSpan = 1;
                RegisterLabels[c].getText();
                c++;
            }
            else
            {
                GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
                RegisterLabels[i] = new Label(shell, SWT.NONE);
                RegisterLabels[i].setLayoutData(gd1);
                RegisterLabels[i].setAlignment(SWT.CENTER);
                gd1.widthHint = 20;
                gd1.horizontalSpan = 1;

            }
        }
    }

    shell.setSize(250, 200);
    status.getText();    
    shell.setText("Running");
    shell.pack();
    centerWindow(shell);
    shell.open();   


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

private void centerWindow(Shell shell) 
{

    Rectangle bds = shell.getDisplay().getBounds();

    Point p = shell.getSize();

    int nLeft = (bds.width - p.x) / 2;
    int nTop = (bds.height - p.y) / 2;

    shell.setBounds(nLeft, nTop, p.x, p.y);
}

@SuppressWarnings("unused")
public static void main(String[] args) {

    Display display = new Display();
    JavaLabelTable ex = new JavaLabelTable(display);
    display.dispose();
}    
}

1 个答案:

答案 0 :(得分:0)

不要调用shell.pack() - 这会将Shell的大小设置为根据其内容大小确定的“首选”大小。

pack()相当于:

setSize(computeSize(SWT.DEFAULT, SWT.DEFAULT, true));