是否有一种正确的方法可以根据按下按钮来修改 SWT window 的内容?

Is there a proper way to modify the contents of a SWT window based on a button press?

“小”模式下的window只有一行文字,一个进度条,打开“日志”,一个取消按钮。当您按下打开“日志”按钮时,它应该显示一个 windows 与上面相同的内容,只是它变成了一个关闭“日志”按钮,并且日志显示出来。

我下面有一段代码不起作用,但据我所知 - 应该。在我制作这段代码之前,我制作了 2 个版本的 window,当你按下 open/close“日志”时,交换被打开。我不喜欢它必须关闭旧的 window 并打开一个新的。

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

这里的问题是,它工作了 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
}

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

在代码的第二次迭代(问题的编辑)中,我做到了,因此每次“更改”只创建一个新的鼠标侦听器。这意味着对于前 2 window 个更改,实际上没有发生任何事情,因为鼠标侦听器都只在 1 个实例中。使用了两次后,需要的鼠标监听器变多了,所以又开始闪烁了。

谢谢!