【问题标题】:How to dispose the progress bar?如何处理进度条?
【发布时间】:2014-12-13 12:43:11
【问题描述】:

我对 SWT 较新。

这是显示进度条的代码。在我的应用程序中单击“取消”按钮时,进度条应该运行。它正在工作..工作结束后也只显示..我工作后如何杀死这个??

                    public static void Pv7(Button ok, Shell shell2) 
                    {
                        shell=shell2;

                        final ProgressBar pb3 = new ProgressBar(shell, SWT.SMOOTH);

                        shell.setLayout(new RowLayout()); 
                        shell.setSize(1500, 1000);

                        pb3.setSelection(500);
                        pb3.setBounds(100, 40, 200, 20);

                        Label label3 = new Label(shell, SWT.NULL);
                        //label2.setText("Success");
                        label3.setAlignment(SWT.LEFT);
                        label3.setBounds(10, 40, 80, 20);
                    }
                        __________________

                My code is displaying the progress after the process also. How to dispose the progress bar the after the another shell window opened??

            This is my main program:

    import org.eclipse.swt.*;
    import org.eclipse.swt.events.*;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.*;
    import org.eclipse.swt.widgets.*;

    public class Practice1 
    {
        static Text text = null;
        final static Shell shell = null;

        public static void main (String [] args) 
        {
            final Display display =new Display();
            final Shell shell=new Shell(display);

            RowLayout rowLayout = new RowLayout();

            shell.setLayout(rowLayout);
                        shell.setLocation(-1,-7);
            shell.setBounds(0, 0, 1050, 720);

            Label label = new Label (shell, SWT.NONE);
            label.setText ("Enter the text:");

            text = new Text (shell, SWT.BORDER);

            final Button ok = new Button (shell, SWT.PUSH);
            ok.setText ("OK");
            ok.addSelectionListener(new SelectionAdapter() 
            {
                @Override
                public void widgetSelected(SelectionEvent e) 
                {

                    PV6.Pv6(ok,shell);
                }
            });

            Button cancel = new Button (shell, SWT.PUSH);
            cancel.setText ("Cancel");
            cancel.addSelectionListener(new SelectionAdapter() 
            {
                @Override
                public void widgetSelected(SelectionEvent e) 
                {
                    System.out.println("Cancel");
                    System.out.println("User cancelled dialog");

                    shell.setVisible(false);
                }
            });
            shell.addListener(SWT.Close, new Listener() 
            {
                    public void handleEvent(Event event) 
                {
                    int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
                    MessageBox messageBox = new MessageBox(shell, style);
                    messageBox.setText("Information");
                    messageBox.setMessage("Close the shell?");
                    event.doit = messageBox.open() == SWT.YES;
                }
                    });

            shell.setDefaultButton (cancel);

            /*print the ID in the console area**/

            ok.addSelectionListener(new SelectionAdapter()
            {
                @Override
                public void widgetSelected(SelectionEvent e)
                {
                    System.out.println(text.getText());
                } 
            });


            //Creating another shell

            ok.addSelectionListener (new SelectionAdapter () 
            {

                @Override
                public void widgetSelected (SelectionEvent e) 
                {
                                            System.out.println(text.getText()); 


                    final Shell dialog = new Shell (shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                    dialog.setText("Dialog Shell");
                    dialog.setBounds(50,100, 10, 10);
                    FormLayout formLayout = new FormLayout ();
                    formLayout.marginWidth = 10;
                    formLayout.marginHeight = 10;

                    dialog.setLayout (formLayout);

                    Label label = new Label (dialog, SWT.NONE);
                    label.setText ("Type a Name:");
                    FormData data = new FormData ();
                    label.setLayoutData (data);

                    Button cancel = new Button (dialog, SWT.PUSH);
                    cancel.setText ("Cancel");

                    data = new FormData ();
                    data.width = 60;
                    data.right = new FormAttachment (100, 0);
                    data.bottom = new FormAttachment (100, 0);

                    cancel.setLayoutData (data);
                    cancel.addSelectionListener (new SelectionAdapter () 
                    {
                        @Override
                        public void widgetSelected (SelectionEvent e) 
                        {

                            System.out.println("User cancelled dialog");
                            dialog.close ();
                        }
                    });

                    text = new Text (dialog, SWT.BORDER);
                    data = new FormData ();
                    data.width = 200;
                    data.left = new FormAttachment (label, 0, SWT.DEFAULT);
                    data.right = new FormAttachment (100, 0);
                    data.top = new FormAttachment (label, 0, SWT.CENTER);
                    data.bottom = new FormAttachment (cancel, 0, SWT.DEFAULT);
                    text.setLayoutData (data);

                    final Button ok = new Button (dialog, SWT.PUSH);
                    ok.setText ("OK");
                    data = new FormData ();
                    data.width = 60;
                    data.right = new FormAttachment (cancel, 0, SWT.DEFAULT);
                    data.bottom = new FormAttachment (100, 0);
                    ok.setLayoutData (data);

                    ok.addSelectionListener (new SelectionAdapter () 
                    {
                        @Override
                        public void widgetSelected (SelectionEvent e)
                        {
                            System.out.println (" " + text.getText ());
                            PV6.Pv6(ok,shell);
                            //  dialog.close ();
                        }
                    });

                    //Creating another shell

                    ok.addSelectionListener (new SelectionAdapter () 
                    {

                        @Override
                        public void widgetSelected (SelectionEvent e) 
                        {
                            //PV6.Pv6(ok,shell);
                            System.out.println(text.getText()); 


                            final Shell dialog = new Shell (shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                            dialog.setText("Dialog Shell");
                            FormLayout formLayout = new FormLayout ();
                            formLayout.marginWidth = 50;
                            formLayout.marginHeight = 10;
                            formLayout.spacing = 100;

                            dialog.setLayout (formLayout);

                            Label label = new Label (dialog, SWT.NONE);
                            label.setText ("Type a Name:");
                            FormData data = new FormData ();
                            label.setLayoutData (data);

                            Button cancel = new Button (dialog, SWT.PUSH);
                            cancel.setText ("Cancel");

                            data = new FormData ();
                            data.width = 60;
                            data.right = new FormAttachment (100, 0);
                            data.bottom = new FormAttachment (100, 0);

                            cancel.setLayoutData (data);
                            cancel.addSelectionListener (new SelectionAdapter () 
                            {
                                @Override
                                public void widgetSelected (SelectionEvent e) 
                                {

                                    System.out.println("User cancelled dialog");
                                    dialog.close ();
                                }
                            });

                            text = new Text (dialog, SWT.BORDER);
                            data = new FormData ();
                            data.width = 200;
                            data.left = new FormAttachment (label, 0, SWT.DEFAULT);
                            data.right = new FormAttachment (100, 0);
                            data.top = new FormAttachment (label, 0, SWT.CENTER);
                            data.bottom = new FormAttachment (cancel, 0, SWT.DEFAULT);
                            text.setLayoutData (data);

                            final Button ok = new Button (dialog, SWT.PUSH);
                            ok.setText ("OK");
                            data = new FormData ();
                            data.width = 60;
                            data.right = new FormAttachment (cancel, 0, SWT.DEFAULT);
                            data.bottom = new FormAttachment (100, 0);
                            ok.setLayoutData (data);

                            ok.addSelectionListener (new SelectionAdapter () 
                            {
                                @Override
                                public void widgetSelected (SelectionEvent e)
                                {
                                    System.out.println ("User typed: " + text.getText ());

                                    PV6.Pv6(ok,shell);
                                    //  dialog.close ();
                                }
                            });                         dialog.setDefaultButton (ok);
                            dialog.pack ();
                            dialog.open ();
                        }
                    });

                    dialog.setDefaultButton (ok);
                    dialog.pack ();
                    dialog.open ();

                    // Move the dialog to the center of the top level shell. 
                    Rectangle shellBounds = shell.getBounds(); 
                    Point dialogSize = dialog.getSize(); 

                    //shell.setBounds(0, 0, 1050, 720);

                    dialog.setLocation( 
                            shellBounds.x + (shellBounds.width - dialogSize.x) / 2, 
                            shellBounds.y + (shellBounds.height -  dialogSize.y) / 2);
                }
            });
            shell.pack ();
            shell.open ();

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

                Any one help me for dispose the progress bar??

【问题讨论】:

  • 正在做的“工作”的代码在哪里?
  • 导入 org.eclipse.swt.*;导入 org.eclipse.swt.events.*;导入 org.eclipse.swt.graphics.Point;导入 org.eclipse.swt.graphics.Rectangle;导入 org.eclipse.swt.layout.*;导入 org.eclipse.swt.widgets.*;公共类练习1 {静态文本文本=空;最终静态壳壳 = null; public static void main (String [] args) { final Display display =new Display();最终壳壳=新壳(显示); RowLayout rowLayout = new RowLayout(); shell.setLayout(rowLayout); shell.setLocation(-1,-7); shell.setBounds(0, 0, 1050, 720);
  • 标签标签=新标签(外壳,SWT.NONE); label.setText("输入文字:");文本 = 新文本(外壳,SWT.BORDER); final Button ok = new Button (shell, SWT.PUSH); ok.setText("OK"); ok.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { PV6.Pv6(ok,shell); } }); shell.pack(); shell.open(); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose(); } }
  • edit您的问题并在那里添加代码,它在 cmets 中不可读。
  • 问题@Baz中添加的代码

标签: java swt


【解决方案1】:

我仍然不能说我完全理解你的问题。我不知道你到底想什么时候处理ProgressBar,但我只会解释你怎么做,然后你决定什么时候调用那个方法:

private static Shell shell;
private static ProgressBar bar;

public static void Pv7(Button ok, Shell shell2) 
{
    shell=shell2;

    if(bar != null && !bar.isDisposed())
        bar.dispose();

    bar = new ProgressBar(shell, SWT.SMOOTH);

    shell.setLayout(new RowLayout()); 
    shell.setSize(1500, 1000);

    pb3.setSelection(500);

    Label label3 = new Label(shell, SWT.NULL);
    label3.setAlignment(SWT.LEFT);
}

public static void disposeProgressBar()
{
    if(bar != null && !bar.idDisposed())
        bar.dispose();
}

然后,当您想要处理 ProgressBar 时,只需调用 disposeProgressBar()

【讨论】:

  • 感谢 Baz 的回答。如果第二个窗口开始显示,那么进度条应该被处理。我认为在错误的地方调用了这个方法?在哪里调用这个方法?
  • @AfshanAhamed 因为我仍然不明白你到底想做什么,所以我真的帮不了你。如果您可以创建一个Minimal, Complete, and Verifiable example(我可以复制/粘贴并运行)并描述期望的结果是什么以及为什么当前结果不是您想要的,那么我可以帮助您。跨度>
  • 单击确定按钮后,另一个窗口开始显示..为此我必须显示进度条..进度条正在显示..到这没问题..在我开始工作后第二个窗口,进度条不应该是可见的。我的代码没有隐藏进度条的目的。这里无法为我的输出粘贴图像.. 请您提供您的邮件 ID 吗?
  • @AfshanAhamed 不,抱歉,我不会泄露我的电子邮件地址。请考虑我在之前的评论中写的内容,否则我将无法为您提供帮助。
  • @巴兹。代码应该从一个只有 SAVE 按钮的空白画布开始。单击“保存”按钮时,应弹出进度查看器并处理至 10 秒。然后应该显示一个弹出窗口,说明保存成功,并带有 OK 和 CLOSE 按钮它应该返回到第一个屏幕,上面只有保存按钮。进度查看器的位置应刚好在保存按钮下方 应传递一个参数来表示进度查看器的显示时间,例如 10 表示 10 秒。如果我们想把它改成15,应该只改变参数值
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 2019-12-09
  • 2017-07-17
  • 1970-01-01
  • 2018-04-28
相关资源
最近更新 更多