Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何控制此shell的大小而不重写它?_Java_Shell_Swt - Fatal编程技术网

Java 如何控制此shell的大小而不重写它?

Java 如何控制此shell的大小而不重写它?,java,shell,swt,Java,Shell,Swt,我需要一些SWT帮助 我用多个get文本标签构建了这个shell。标签控制外壳的大小,但我希望外壳为250x200像素。在我打开外壳之前,我已经定好了尺寸。如何防止它被覆盖?我希望最终能够放入1000行,并能够向下滚动,而不是有一个巨大的窗口,我不能做任何事情 import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.ec

我需要一些SWT帮助

我用多个get文本标签构建了这个shell。标签控制外壳的大小,但我希望外壳为250x200像素。在我打开外壳之前,我已经定好了尺寸。如何防止它被覆盖?我希望最终能够放入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();
}    
}
import org.eclipse.swt.swt;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Shell;
导入org.eclipse.swt.layout.GridData;
导入org.eclipse.swt.layout.GridLayout;
导入org.eclipse.swt.widgets.Label;
导入org.eclipse.swt.graphics.Point;
导入org.eclipse.swt.graphics.Rectangle;
导入org.eclipse.swt.graphics.Color;
公共类JavaLabelTable{
自有品牌地位;
私人显示器;
私有外壳=新外壳(显示,SWT.DIALOG|u TRIM | SWT.CENTER | SWT.V|u滚动);
公共JavaLabelTable(显示)
{
初始化(显示);
}
公共void initUT(显示)
{
int q=110;
int w=40;
int e=q-w;
int i;
int y=120;
int b=1;
int c=1;
shell.setLayout(新的GridLayout(3,false));
标签注册器标签[]=新标签[y];
状态=新标签(外壳、SWT.边框);
GridData gd2=新的GridData(SWT.FILL,SWT.FILL,true,true);
状态。setLayoutData(gd2);
gd2.1=q;
gd2.heightHint=15;
gd2.水平跨度=3;

对于(i=1;i不要调用
shell.pack()
),这会将shell的大小设置为根据其内容大小确定的“首选”大小

pack()
相当于:

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

谢谢。我确实去掉了它,但窗口最终缩小了外壳内的内容。我指定了标签大小,但缩小窗口会调整其中的所有内容。如何锁定外壳内内容的大小?使用GridLayout之类的工具的目的是在调整外壳大小时调整控件的大小。在任何case您可能应该在此处使用
,而不是尝试使用标签重新创建它。我以前尝试过。我在使用asyncexec时遇到问题。因此我采用了此方法。但我可以再试一次。至少该表会自动创建一个滚动条。