Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每次打开一个外壳_Java_Shell_Swt - Fatal编程技术网

Java 每次打开一个外壳

Java 每次打开一个外壳,java,shell,swt,Java,Shell,Swt,在我的项目中,我有一个shell,shell中有3个按钮,我希望单击每个按钮将打开一个shell,但我希望如果一个shell由于单击按钮而已经打开,那么该shell将关闭,并打开一个新shell。 (我不希望单击按钮时同时打开两个外壳) 但我不知道怎么做 在该类中,壳的开口应为 public class ClickLabel implements MouseListener { Shell shell; int p; public ClickLab

在我的项目中,我有一个shell,shell中有3个按钮,我希望单击每个按钮将打开一个shell,但我希望如果一个shell由于单击按钮而已经打开,那么该shell将关闭,并打开一个新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();              
        }
}

有人能帮我吗?

这是一个简单的示例,带有按钮和一个活动的
外壳,请检查:

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 ();
    }
}
import org.eclipse.swt.swt;
导入org.eclipse.swt.events.SelectionAdapter;
导入org.eclipse.swt.events.SelectionEvent;
导入org.eclipse.swt.layout.FillLayout;
导入org.eclipse.swt.widgets.Button;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Shell;
公开课范例{
公共静态void main(字符串[]args){
新例子();
}
私有Shell openedShell;
公共示例(){
最终显示=新显示();
外壳=新外壳(显示);
setLayout(新的FillLayout());
SelectionAdapter=新建SelectionAdapter(){
@凌驾
公共无效WidgeSelected(SelectionEvent e){
if(openedShell!=null){
openedShell.dispose();
}
openedShell=新Shell(显示);
openedShell.setSize(200200);
openedShell.setText(((按钮)e.getSource()).getText());
openedShell.open();
}
};

对于(int i=1;我不知道该怎么做,这就是我问的原因。显而易见的答案是,你需要跟踪你打开的外壳-点击哪个按钮打开每个外壳,以及它们处于哪个状态。但这不是一个要求人们从头开始为你实现东西的地方,所以你尝试了什么?