有没有合适的方法可以根据按钮按下来修改SWT窗口的内容



;"小";模式只有一行文本,一个进度条,打开";日志";,以及一个取消按钮。当你按下"打开"按钮时;日志";按钮时,它应该显示具有与上面相同内容的窗口,除了它变为"关闭";日志";按钮,日志就会显示出来。

我在下面有一段代码不起作用,但据我所知应该起作用。在我编写这段代码之前,我制作了两个版本的窗口,当你按下打开/关闭";日志";。我不喜欢它不得不关上旧窗户再打开一扇新窗户。

除了下面的故障代码之外,还有更好的方法吗?

这里的问题是,它工作0.1秒,然后在状态1和状态2之间循环变化。然后它说";没有响应";在大约5秒之后。

package main;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
public class ProcessBar {
protected Shell shlApplicationName = new Shell();
private boolean logs = false;
public static void main(String[] args) {
try {
ProcessBar window = new ProcessBar();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
Display display = Display.getDefault();
launch();
shlApplicationName.open();
shlApplicationName.layout();
while (!shlApplicationName.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

Label lblGeneratingWhat = new Label(shlApplicationName, SWT.NONE);
ProgressBar progressBar = new ProgressBar(shlApplicationName, SWT.NONE);
Button btnCancel = new Button(shlApplicationName, SWT.NONE);
Text text = new Text(shlApplicationName, SWT.BORDER | SWT.V_SCROLL);
Button btnLogs = new Button(shlApplicationName, SWT.NONE);

private void launch() {

shlApplicationName.setText("Application Name");
if (logs) {
bigSizes();
}else {
smallContents();
}

}

private void bigSizes() {

shlApplicationName.setSize(450, 247);
lblGeneratingWhat.setBounds(10, 10, 414, 15);
lblGeneratingWhat.setText("Generating What?");
progressBar.setBounds(10, 31, 414, 29);
btnCancel.setBounds(349, 173, 75, 25);
btnCancel.setText("Cancel");
text.setEditable(false);
text.setBounds(10, 66, 333, 132);

btnLogs.setBounds(349, 142, 75, 25);
btnLogs.setText("Logs");

btnLogs.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
logs = !logs;
launch();
}
});
}

private void smallContents() {

shlApplicationName.setSize(450, 138);

lblGeneratingWhat.setBounds(10, 10, 414, 15);
lblGeneratingWhat.setText("Generating What?");

progressBar.setBounds(10, 31, 414, 29);

btnCancel.setBounds(349, 66, 75, 25);
btnCancel.setText("Cancel");

text.setBounds(0, 0, 0, 0);

btnLogs.setBounds(268, 66, 75, 25);
btnLogs.setText("Logs");

btnLogs.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
logs = !logs;
launch();
}
});

}
}

编辑:这段新代码工作得更好,但在";日志";按钮

它在你打开日志的前两次,然后关闭它时正常工作。之后,它开始以与上面代码相同的方式出现故障,每次按下的时间都在增加。

package main;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
public class ProcessBar {
protected Shell shlApplicationName = new Shell();
private boolean logs = false;
public static void main(String[] args) {
try {
ProcessBar window = new ProcessBar();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
Display display = Display.getDefault();
launch();
shlApplicationName.open();
shlApplicationName.layout();
while (!shlApplicationName.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

Label lblGeneratingWhat = new Label(shlApplicationName, SWT.NONE);
ProgressBar progressBar = new ProgressBar(shlApplicationName, SWT.NONE);
Button btnCancel = new Button(shlApplicationName, SWT.NONE);
Text text = new Text(shlApplicationName, SWT.BORDER | SWT.V_SCROLL);
Button btnLogs = new Button(shlApplicationName, SWT.NONE);
Button btn2Logs = new Button(shlApplicationName, SWT.NONE);

private void launch() {

shlApplicationName.setText("Application Name");
if (logs) {
bigSizes();
}else {
smallContents();
}


}

private void bigSizes() {

shlApplicationName.setSize(450, 247);
lblGeneratingWhat.setBounds(10, 10, 414, 15);
lblGeneratingWhat.setText("Generating Whatdog?");
progressBar.setBounds(10, 31, 414, 29);
btnCancel.setBounds(349, 173, 75, 25);
btnCancel.setText("Cancel");
text.setEditable(false);
text.setBounds(10, 66, 333, 132);

btnLogs.setBounds(0, 0, 0, 0);

btn2Logs.setBounds(349, 142, 75, 25);
btn2Logs.setText("Logs");

btn2Logs.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
logs = !logs;
launch();

}
});
}

private void smallContents() {

shlApplicationName.setSize(450, 138);

lblGeneratingWhat.setBounds(10, 10, 414, 15);
lblGeneratingWhat.setText("Generating What?");

progressBar.setBounds(10, 31, 414, 29);

btnCancel.setBounds(349, 66, 75, 25);
btnCancel.setText("Cancel");

text.setBounds(0, 0, 0, 0);

btn2Logs.setBounds(0, 0, 0, 0);

btnLogs.setBounds(268, 66, 75, 25);
btnLogs.setText("Logs");

btnLogs.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
logs = !logs;
launch();
}
});

}
}

我是如何修复的。

Greg-449在对问题的评论中就如何解决这个问题提出了建议。以下是我如何引用";固定的";

代码:

package main;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
public class ProcessBar {
protected Shell shlApplicationName = new Shell();

public static void main(String[] args) {
try {
ProcessBar window = new ProcessBar();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
Display display = Display.getDefault();
prelaunch();
shlApplicationName.open();
shlApplicationName.layout();
while (!shlApplicationName.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

Label lblGeneratingWhat = new Label(shlApplicationName, SWT.NONE);
ProgressBar progressBar = new ProgressBar(shlApplicationName, SWT.NONE);
Button btnCancel = new Button(shlApplicationName, SWT.NONE);
Text text = new Text(shlApplicationName, SWT.BORDER | SWT.V_SCROLL);

Button btnLogs = new Button(shlApplicationName, SWT.NONE);
private boolean logs = false;
private void prelaunch() {

launch();

btnLogs.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
logs = !logs;
launch();
}
});

}

private void launch() {

shlApplicationName.setText("Application Name");

if (logs) {
bigSizes();
}else {
smallContents();
}   

}

private void bigSizes() {

shlApplicationName.setSize(450, 247);
lblGeneratingWhat.setText("Generating Whatdog?");
progressBar.setBounds(10, 31, 414, 29);
btnCancel.setBounds(349, 173, 75, 25);
text.setVisible(true);
text.setEditable(false);
text.setBounds(10, 66, 333, 132);

btnLogs.setBounds(349, 142, 75, 25);


}

private void smallContents() {

shlApplicationName.setSize(450, 138);

lblGeneratingWhat.setBounds(10, 10, 414, 15);
lblGeneratingWhat.setText("Generating What?");

progressBar.setBounds(10, 31, 414, 29);

btnCancel.setBounds(349, 66, 75, 25);
btnCancel.setText("Cancel");

text.setVisible(false);
btnLogs.setBounds(268, 66, 75, 25);
btnLogs.setText("Logs");


}
}

不同的是,我确保不会创建一个新的mouselistener来监听同一个按钮。代替(伪码(

function launch{
create mouse listener
if logs is true then open logs, else open no logs version
open launch
}

我把它变成了:(伪码(

function prelaunch{
create mouse listener
open launch
}
function launch{
if logs is true then open logs, else open no logs version
open launch
}

之所以会出现闪烁问题,是因为调用了一个新的鼠标侦听器,该侦听器会翻转布尔值并重新启动应用程序。在我的第一次代码迭代中,这导致在我按下按钮时创建了一个新的鼠标侦听器循环。

在代码的第二次迭代(问题编辑(中;改变";只创建一个新的鼠标侦听器。这意味着,对于前两个窗口的更改,没有发生任何实际的事情,因为鼠标侦听器都只在一个实例上。两次使用后,需要更多的鼠标侦听器,因此它再次开始闪烁。

谢谢!

最新更新