gpt4 book ai didi

java - SWT FileDialog 打开问题

转载 作者:行者123 更新时间:2023-12-02 00:08:31 26 4
gpt4 key购买 nike

我需要打开一个 FileDialog,以便在使用 Java 7 的 Windows 7 Home 上使用 SWT 打开文件。我需要在按下 BrowseBtn1 按钮 (SWT) 时打开此 FileDialog。我为此使用 addSelectionListener 。这是正确的方法吗?如果是,为什么我的对话框在没有遇到任何运行时错误时无法打开。我根本不使用 AWT,只使用 SWT。

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.*;

public class MySwtApp {
public static Display display;
public static Shell shell;

public static void main(String[] args) {

display = new Display ();
shell = new Shell (display);
shell.setText("MY TITLE");
GridLayout gridLayout = new GridLayout (3, false);
shell.setLayout (gridLayout);

Label Label1 = new Label (shell, SWT.NONE);
Label1.setText ("Select Message ");
GridData data = new GridData (200, SWT.DEFAULT);
Label1.setLayoutData (data);

Combo Combo1 = new Combo (shell, SWT.NONE);
Combo1.setItems (new String [] {"Option A",
"Option B"});
// CaseStudyCombo.setText ("CaseStudyCombo");
data = new GridData (200, SWT.DEFAULT);
Combo1.setLayoutData (data);
Combo1.addListener (SWT.DefaultSelection, new Listener () {
public void handleEvent (Event e) {
System.out.println (e.widget + " - Default Selection");
}
});

Label emptyLabel = new Label (shell, SWT.NONE);
emptyLabel.setText ("");
data = new GridData (50, SWT.DEFAULT);
emptyLabel.setLayoutData (data);

Label Label2 = new Label (shell, SWT.NONE);
Label2.setText ("Next Message: ");
data = new GridData (200, SWT.DEFAULT);
Label2.setLayoutData (data);

final Text text4 = new Text (shell, SWT.BORDER);
text4.setText ("");
data = new GridData (215, SWT.DEFAULT);
text4.setLayoutData (data);

Button BrowseBtn1 = new Button (shell, SWT.PUSH);
BrowseBtn1.setText ("Browse");
data = new GridData (80, SWT.DEFAULT);
BrowseBtn1.setLayoutData (data);
BrowseBtn1.addSelectionListener(new SelectionAdapter() {
String result = "";
public void WidgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog (shell, SWT.OPEN);
dialog.setFilterExtensions(new String [] {"*.html"});
//dialog.setFilterPath("c:\\temp");
result = dialog.open();
text4.setText(result);
}
});

Label Label3 = new Label (shell, SWT.NONE);
Label3.setText ("Message Label 3: ");
data = new GridData (200, SWT.DEFAULT);
Label3.setLayoutData (data);

Text text5 = new Text (shell, SWT.BORDER);
text5.setText ("");
data = new GridData (215, SWT.DEFAULT);
text5.setLayoutData (data);

Button BrowseBtn2 = new Button (shell, SWT.PUSH);
BrowseBtn2.setText ("Browse");
data = new GridData (80, SWT.DEFAULT);
BrowseBtn2.setLayoutData (data);

Label Label4 = new Label (shell, SWT.NONE);
Label4.setText ("Message Label 4: ");
data = new GridData (200, SWT.DEFAULT);
Label4.setLayoutData (data);

Combo Combo2 = new Combo (shell, SWT.NONE);
Combo2.setText ("Options");
data = new GridData (200, SWT.DEFAULT);
Combo2.setLayoutData (data);

Button searchBtn1 = new Button (shell, SWT.PUSH);
searchBtn1.setText ("Search");
data = new GridData (80, SWT.DEFAULT);
searchBtn1.setLayoutData (data);

Combo Combo3 = new Combo (shell, SWT.NONE);
// CaseStudyCombo.setItems (new String [] {"Item 1", "Item 2", "Item 2"});
Combo3.setText ("Options:");
data = new GridData (200, SWT.DEFAULT);
Combo3.setLayoutData (data);

Combo Combo4 = new Combo (shell, SWT.NONE);
Combo4.setText ("Options:");
data = new GridData (200, SWT.DEFAULT);
Combo4.setLayoutData (data);

Button showDetailsBtn = new Button (shell, SWT.PUSH);
showDetailsBtn.setText ("Show Details");
data = new GridData (80, SWT.DEFAULT);
showDetailsBtn.setLayoutData (data);

shell.pack ();
shell.open ();

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

请帮忙解决这个问题。

最佳答案

有问题的行是这样的:

public void WidgetSelected(SelectionEvent e)

注意大写的“W”。您当前正在做的是在该匿名类中定义一个新方法(而不是覆盖现有的 widgetSelected(...) 方法)。如果您将其更改为小写“w”,如下所示,它将起作用:

public void widgetSelected(SelectionEvent e)

使用@Override注释是一个很好的实践(实际上比推荐的更好)。如果您使用了它,编译器会标记您一个错误(例如,请参阅 Joshua Bloch Effect of Java 第 2 版第 36 项:一致使用 Override 注释)

关于java - SWT FileDialog 打开问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com