2011-12-02 25 views
0

我正在做一个教程,并且我自己添加了一些标签来测试它,并且抛出一些东西来获得更好的抓握效果和labellast。 setLocation似乎只是做没什么labellast,标签的位置不会改变......从eclipse学习SWT开始,为什么这个标签不会改变位置

这里是我的源:

public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setSize(250, 350); 

    Label label1 = new Label(shell, SWT.BORDER); 
    label1.setText("Hello World!"); 
    label1.setSize(100, 20); 
    label1.setLocation(30, 30); 

    Text text1 = new Text(shell, SWT.BORDER); 
    text1.setText("Type Here"); 
    text1.setTextLimit(50); 
    text1.setBounds(10, 10, 200, 20); 
    text1.setLocation(30, 60); 

    Label hello = new Label(shell, SWT.BORDER); 
    hello.setText("type what the top line says."); 
    hello.setSize(190, 20); 
    hello.setLocation(30, 90); 

    Label sep1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL 
      | SWT.SHADOW_IN); 
    sep1.setBounds(30, 60, 100, 120); 
    Label label2 = new Label(shell, SWT.NONE); 
    label2.setText("World Hello"); 
    label2.setSize(100, 20); 
    label2.setLocation(30, 150); 

    Label sep2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
    sep2.setBounds(30, 120, 100, 20); 

    Label label3 = new Label(shell, SWT.NONE); 
    label3.setSize(100, 20); 
    label3.setLocation(30, 210); 
    label3.setBackground(new Color(display, 200, 111, 50)); 
    label3.setText("World World."); 

    Text text2 = new Text(shell, SWT.NONE); 
    text2.setEchoChar('*'); 
    text2.setBounds(10, 50, 200, 20); 
    text2.setText("Password"); 
    text2.setLocation(30, 250); 
    text2.setEditable(false); 

    Label labellast = new Label(shell, SWT.NONE); 
    labellast.setLocation(30, 300); 
    labellast.setText("You cant edit that^^^"); 
    labellast.setBounds(10, 50, 200, 20); 

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

    } 
    display.dispose(); 
} 

} 

回答

1

setBounds同时设置位置和大小的方法。

setBounds(int x, int y, int width, int height) 

摆脱调用到setLocation,只需使用setBounds

labellast.setText("You cant edit that^^^"); 
labellast.setBounds(30, 300, 200, 20); 

在你的代码的调用setLocation第一,然后再覆盖的位置,当你调用setBounds这使得它看起来像你不能改变位置。

+0

谢谢!本教程在这方面有点误导,因为它一直告诉我有一个设置的位置和每一个的界限......我很困惑,所有四个参数setBounds做了什么。 – poetzmij

相关问题