SWT FileDialog error: constructor FileDialog is undefined
SWT FileDialog error: constructor FileDialog is undefined
我刚开始学习 Java 基础知识,在使用 SWT FileDialog
时遇到了这个问题。 Eclipse 总是 returns 这个错误:The constructor FileDialog(Shell, int)
is undefined
完整代码如下,请各位指教?
public class FormObject2 {
protected Shell shell;
private Text txtComboBoxItem;
private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
private Text taOne;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
FormObject2 window = new FormObject2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(481, 364);
shell.setText("SWT Application");
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmOpen = new MenuItem(menu_1, SWT.NONE);
mntmOpen.setAccelerator(1);
mntmOpen.setText("Open");
mntmOpen.setAccelerator(SWT.MOD1+ 'O');
MenuItem mntmSave = new MenuItem(menu_1, SWT.NONE);
mntmSave.setAccelerator(1);
mntmSave.setText("Save");
MenuItem mntmEdit = new MenuItem(menu, SWT.CASCADE);
mntmEdit.setText("Edit");
Menu menu_2 = new Menu(mntmEdit);
mntmEdit.setMenu(menu_2);
MenuItem mntmExit = new MenuItem(menu_2, SWT.NONE);
mntmExit.setText("Exit");
mntmOpen.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shell, SWT.MULTI);
dialog.setFilterPath("C:\Logs");
dialog.setFilterExtensions(new String[]{"*.txt", "*.pdf", "*.*"});
dialog.setFilterNames(new String[]{ "Rich Text Format", "HTML Document", "Any"});
dialog.open();
}
});
}
您可能导入了错误的 FileDialog
class - 您需要导入 org.eclipse.swt.widgets.FileDialog
而不是 java.awt.FileDialog
我刚开始学习 Java 基础知识,在使用 SWT FileDialog
时遇到了这个问题。 Eclipse 总是 returns 这个错误:The constructor FileDialog(Shell, int)
is undefined
完整代码如下,请各位指教?
public class FormObject2 {
protected Shell shell;
private Text txtComboBoxItem;
private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
private Text taOne;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
FormObject2 window = new FormObject2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(481, 364);
shell.setText("SWT Application");
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmOpen = new MenuItem(menu_1, SWT.NONE);
mntmOpen.setAccelerator(1);
mntmOpen.setText("Open");
mntmOpen.setAccelerator(SWT.MOD1+ 'O');
MenuItem mntmSave = new MenuItem(menu_1, SWT.NONE);
mntmSave.setAccelerator(1);
mntmSave.setText("Save");
MenuItem mntmEdit = new MenuItem(menu, SWT.CASCADE);
mntmEdit.setText("Edit");
Menu menu_2 = new Menu(mntmEdit);
mntmEdit.setMenu(menu_2);
MenuItem mntmExit = new MenuItem(menu_2, SWT.NONE);
mntmExit.setText("Exit");
mntmOpen.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shell, SWT.MULTI);
dialog.setFilterPath("C:\Logs");
dialog.setFilterExtensions(new String[]{"*.txt", "*.pdf", "*.*"});
dialog.setFilterNames(new String[]{ "Rich Text Format", "HTML Document", "Any"});
dialog.open();
}
});
}
您可能导入了错误的 FileDialog
class - 您需要导入 org.eclipse.swt.widgets.FileDialog
而不是 java.awt.FileDialog