【问题标题】:Opening one shell each time每次打开一个外壳
【发布时间】:2013-12-26 11:53:44
【问题描述】:

在我的项目中,我有一个外壳,外壳中有 3 个按钮,我希望单击每个按钮都会打开一个外壳,但我希望如果外壳由于单击按钮而已经打开,那么该外壳将被关闭并打开一个新外壳。 (我不希望点击按钮的 2 个 shell 同时打开) 但我不知道该怎么做。

在这个类中开壳应该是。

public class ClickLabel implements MouseListener
{
        Shell shell;
        int p;
        public ClickLabel(int p)
        {
            shell = new Shell();
            this.p = p;
        }   
        @Override
        public void mouseDoubleClick(MouseEvent e) {}
        @Override
        public void mouseDown(MouseEvent e) {}
        @Override
        public void mouseUp(MouseEvent e) {
             shell.open();              
        }
}

谁能帮帮我?

【问题讨论】:

  • 我不知道该怎么做,所以我才问。
  • 显而易见的答案是,您需要跟踪您打开的 shell - 单击了哪个按钮来打开每个 shell,以及它们处于哪种状态。这不是一个要求人们这样做的地方不过,为您从头开始实施一些东西,那么您尝试过什么?

标签: java shell swt


【解决方案1】:

这是一个简单的例子,带有按钮和一个活动的Shell,检查一下:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Example{
    public static void main(String[] args) {
        new Example();
    }

    private Shell openedShell;

    public Example() {
        final Display display = new Display ();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        SelectionAdapter adapter = new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(openedShell != null){
                    openedShell.dispose();
                }
                openedShell = new Shell(display);
                openedShell.setSize(200,200);
                openedShell.setText(((Button)e.getSource()).getText());
                openedShell.open();
            }
        };
        for(int i =1;i<4;i++){
            Button b = new Button(shell, SWT.PUSH); 
            b.setText("shell "+i);
            b.addSelectionListener(adapter); 
            b.pack();
        }

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

【讨论】:

    猜你喜欢
    • 2010-10-10
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多