Display display = Display.getDefault(); Shell shell = new Shell(); shell.setSize(450, 300); shell.setText("SWT Application"); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } }没有出现窗口
时间: 2023-08-12 19:04:33 浏览: 35
根据你给出的代码,这是一段使用 SWT 库创建 GUI 窗口的代码。根据代码来看,应该可以正常显示窗口的。如果没有出现窗口,可能是以下几个原因之一:
1. 代码没有被正确执行。请确保代码能够被正确编译和执行,或者尝试使用其他的 IDE 或工具来运行代码。
2. 显示器或操作系统问题。请确保你的显示器能够正常工作,并且操作系统没有出现任何问题。
3. 程序逻辑问题。请检查代码是否有逻辑错误或其他问题。你可以通过添加调试语句或使用调试工具来帮助你找出问题所在。
相关问题
eclipse便签代码
以下是Eclipse中创建便签代码的步骤:
1. 创建一个新的Java项目。
2. 在src文件夹中创建一个新的Java类。
3. 在Java类中添加以下代码:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class NotePad {
protected Shell shell;
private Text text;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
NotePad window = new NotePad();
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(450, 300);
shell.setText("Notepad");
text = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
text.setBounds(10, 10, 414, 242);
}
}
```
4. 运行程序,将会看到一个简单的便签应用程序。
在SWT的Label中点击实现倒计时,当倒计时结束时再次点击Label框再次开始倒计时,当倒计时没有结束时再次点击Label框会在控制面板输出'请等待'
这是一个可能的实现方式:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class CountDownLabel {
private static final int COUNTDOWN_SECONDS = 10;
private static final Color COLOR_DEFAULT = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
private static final Color COLOR_WAITING = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
private static final Font FONT_DEFAULT = new Font(Display.getDefault(), new FontData("Arial", 24, SWT.BOLD));
private static final Font FONT_WAITING = new Font(Display.getDefault(), new FontData("Arial", 24, SWT.NORMAL));
private Display display;
private Shell shell;
private Label label;
private int countdownSeconds;
private boolean isCountingDown;
public CountDownLabel() {
display = new Display();
shell = new Shell(display);
shell.setText("CountDown Label");
shell.setLayout(new GridLayout());
label = new Label(shell, SWT.CENTER);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
label.setForeground(COLOR_DEFAULT);
label.setFont(FONT_DEFAULT);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
if (isCountingDown) {
System.out.println("请等待");
} else {
startCountdown();
}
}
});
countdownSeconds = 0;
isCountingDown = false;
}
public void open() {
shell.setSize(300, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
FONT_DEFAULT.dispose();
FONT_WAITING.dispose();
display.dispose();
}
private void startCountdown() {
countdownSeconds = COUNTDOWN_SECONDS;
isCountingDown = true;
label.setForeground(COLOR_WAITING);
label.setFont(FONT_WAITING);
updateLabel();
new Thread(() -> {
while (countdownSeconds > 0) {
countdownSeconds--;
display.asyncExec(this::updateLabel);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
display.asyncExec(this::resetLabel);
}).start();
}
private void updateLabel() {
label.setText(String.valueOf(countdownSeconds));
}
private void resetLabel() {
isCountingDown = false;
label.setForeground(COLOR_DEFAULT);
label.setFont(FONT_DEFAULT);
label.setText("Click to start countdown");
}
public static void main(String[] args) {
CountDownLabel countDownLabel = new CountDownLabel();
countDownLabel.open();
}
}
```
这个程序创建了一个窗口,其中包含一个Label控件。当用户点击Label时,如果倒计时正在进行,则控制面板输出“请等待”,否则开始倒计时。倒计时从10秒开始,每秒更新Label的文本,直到计时结束。在计时结束后,Label会回到初始状态,等待下一次点击开始新的倒计时。在计时过程中,Label的前景色变为灰色,字体变为普通,以表示当前不可用。