我是一个初学者啊,代码如上而且是照书上的敲上去的。认真检查了都没错,也导入了SWT包。

package eclipseSwt.swt;

import java.awt.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class EclipseSwt {
public static void main(String[] args){
Display display=new Display();
Shell shell=new Shell(display);
shell.setText("窗口");
shell.setBounds(200, 200, 500, 350);
FormLayout layout=new FormLayout();
layout.marginHeight=50;
layout.marginWidth=50;
shell.setLayout(layout);
Label helloLabel = new Label(shell,SWT.CENTER);

helloLabel.setText("你好,SWT");
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

}

为什么编译的时候总是说
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor Label(Shell, int) is undefined

at eclipseSwt.swt.EclipseSwt.main(EclipseSwt.java:19)

这是为什么啊??

富国沪深
浏览 74回答 4
4回答

慕丝7291255

你把import java.awt.Label; 删除,然后CTRL+S保存,再按CTRL+SHIFT+O,注意是字母O如果Label helloLabel = new Label(shell,SWT.CENTER);这行前面还是有红XX的话,证明你的这个构造函数不存在,要么是你输入错误,要么是书上写错了。

婷婷同学_

可能Label导入错误,先删除在重新导入试下把import java.awt.Label; 删除然后CTRL+S保存再按CTRL+SHIFT+O,注意是字母O如果Label helloLabel = new Label(shell,SWT.CENTER);这行前面还是有红XX的话,证明这个构造函数不存在,要么是你输入错误,要么是书上写错了。

30秒到达战场

好像import java.awt.Label中的这个label没有你Label helloLabel = new Label(shell,SWT.CENTER);这个构造函数,看看你用的这个label是不是你需要的那个。你用这个包中的label只有三个构造函数Label() ,Label(String text) ,Label(String text, int alignment)

呼啦一阵风

代码 Label helloLabel = new Label(shell,SWT.CENTER) 中;label在swt 中导入包为-> import org.eclipse.swt.widgets.Label; 在awt 中导入包为 ->import java.awt.Label; 需要区分两个包的引入。根据你粘贴的代码中判断,你引入的包为 import java.awt.Label; awt包下没有Label(shell,SWT.CENTER)的构造器定义,正确引入包为 import org.eclipse.swt.widgets.Label。
打开App,查看更多内容
随时随地看视频慕课网APP