Java SWT - 可调整大小 shell 并具有自定义形状

Java SWT - Resizable shell with custom shape

我有一个应用程序,您可以在其中召唤带有控件的小 windows。我希望这些 windows 是带圆角的矩形,没有任何装饰。我发现了这个老问题并使用了它: SWT Shell with round corners.

我最终得到了这个:

package swt_window_test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;


public class SingleDisplayMultipleShells
{

    // circle
    static int[] circle(int r, int offsetX, int offsetY) {
        int[] polygon = new int[8 * r + 4];
        // x^2 + y^2 = r^2
        for (int i = 0; i < 2 * r + 1; i++) {
          int x = i - r;
          int y = (int) Math.sqrt(r * r - x * x);
          polygon[2 * i] = offsetX + x;
          polygon[2 * i + 1] = offsetY + y;
          polygon[8 * r - 2 * i - 2] = offsetX + x;
          polygon[8 * r - 2 * i - 1] = offsetY - y;
        }
        return polygon;
      }


  // ========================================
  // Inner class to represent a child Shell
  // ========================================
  private class ChildShell
  {
     public ChildShell(Shell mainWindowShell)
     {
        System.out.println("Creating new child Shell");
        Display display = mainWindowShell.getDisplay();

        // =========================================
        // Create a Shell (window) from the Display
        // =========================================
        final Shell shell = new Shell(mainWindowShell, SWT.NO_TRIM | SWT.ON_TOP);

        shell.setBackground(display.getSystemColor(SWT.COLOR_RED));

        // define a region that looks like a key hole
        Region region = new Region();

        region.add(circle(10, 10, 10));
        region.add(circle(10, 10, 500));
        region.add(circle(10, 500, 10));
        region.add(circle(10, 500, 500));
        region.add(new int[] { 10, 0, 10, 510, 500, 510, 500, 0 });
        region.add(new int[] { 0, 10, 0, 500, 510, 500, 510, 10 });

        // define the shape of the shell using setRegion
        shell.setRegion(region);
        Rectangle size = region.getBounds();
        shell.setSize(size.width, size.height);
        // add ability to move shell around
        Listener l = new Listener() {
          Point origin;

          public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.MouseDown:
              origin = new Point(e.x, e.y);
              break;
            case SWT.MouseUp:
              origin = null;
              break;
            case SWT.MouseMove:
              if (origin != null) {
                Point p = display.map(shell, null, e.x, e.y);
                shell.setLocation(p.x - origin.x, p.y - origin.y);
              }
              break;
            }
          }
        };
        shell.addListener(SWT.MouseDown, l);
        shell.addListener(SWT.MouseUp, l);
        shell.addListener(SWT.MouseMove, l);
        // add ability to close shell
        Button b = new Button(shell, SWT.PUSH);
        b.setBackground(shell.getBackground());
        b.setText("close");
        b.pack();
        b.setLocation(10, 68);
        b.addListener(SWT.Selection, new Listener() {
          public void handleEvent(Event e) {
            shell.close();
          }
        });
        shell.open();

        // =============================================================
        // Register a listener for the Close event on the child Shell.
        // This disposes the child Shell
        // =============================================================
        shell.addListener(SWT.Close, new Listener()
        {
           @Override
           public void handleEvent(Event event)
           {
              System.out.println("Child Shell handling Close event, about to dispose this Shell");
              shell.dispose();
           }
        });
     }
  }

  public SingleDisplayMultipleShells()
  {
     // ======================================================
     // Create the main Display object that represents the UI
     // subsystem and contains the single UI handling thread
     // ======================================================
     final Display display = Display.getDefault();

     // ====================================================
     // create a shell for the main window from the Display
     // ====================================================
     final Shell mainWindowShell = new Shell(display, SWT.CLOSE);

     // =====================
     // Set the Window Title
     // =====================
     mainWindowShell.setText("Main Shell");

     // =========================================
     // Create a button that spawns child Shells
     // =========================================
     Button spawn = new Button(mainWindowShell, SWT.PUSH);
     spawn.setText("Create Child");
     spawn.setBounds(10, 10, 150, 30);
     spawn.addSelectionListener(new SelectionListener()
     {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
           // =====================================================
           // on button press, create a child Shell object passing
           // the main Display. The child could also access the
           // display itself by calling Display.getDefault()
           // =====================================================
           System.out.println("Main Shell handling Button press, about to create child Shell");
           new ChildShell(mainWindowShell);           
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e)
        {
           widgetSelected(e);
        }
     });

     // =============================================================
     // Register a listener for the Close event on the main Shell.
     // This disposes the Display which will cause the entire child
     // tree to dispose
     // =============================================================
     mainWindowShell.addListener(SWT.Close, new Listener()
     {
        @Override
        public void handleEvent(Event event)
        {
           System.out.println("Main Shell handling Close event, about to dipose the main Display");
           display.dispose();
        }
     });

     // ================================
     // Set size on main Shell and open
     // ================================
     mainWindowShell.setSize(200, 200);
     mainWindowShell.open();

     // =====================================
     // Main UI event dispatch loop
     // that handles all UI events from all
     // SWT components created as children of
     // the main Display object
     // =====================================
     while (!display.isDisposed())
     {
        // ===================================================
        // Wrap each event dispatch in an exception handler
        // so that if any event causes an exception it does
        // not break the main UI loop
        // ===================================================
        try
        {
           if (!display.readAndDispatch())
           {
              display.sleep();
           }
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
     }

     System.out.println("Main Display event handler loop has exited");
  }

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

我对结果很满意,但我想知道是否有办法让这些 windows 可调整大小?

正如 greg 在评论中所说,我想做的是需要使用 SWT.RESIZE,它与 SWT.NO_TRIM 不兼容,没有第二个我就无法自定义形状一.

所以我所做的就是添加一个按钮来改变 shell:

的形状
Button resize = new Button(shell, SWT.PUSH);
resize.setText("Resize");
resize.setBounds(140, 430, 20, 20);
resize.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event e) {
        if (!isMaximized) {
            Region region = new Region();
            region.add(circle(20, 20, 20));
            region.add(circle(20, 20, 580));
            region.add(circle(20, 280, 20));
            region.add(circle(20, 280, 580));
            region.add(new int[] { 20, 0, 280, 0, 280, 600, 20, 600 });
            region.add(new int[] { 0, 20, 300, 20, 300, 580, 0, 580 });
            shell.setRegion(region);
            Rectangle size = region.getBounds();
            shell.setSize(size.width, size.height);
            resize.setBounds(140, 580, 20, 20);
            isMaximized = true;
        }else {
            Region region = new Region();
            region.add(circle(20, 20, 20));
            region.add(circle(20, 20, 430));
            region.add(circle(20, 280, 20));
            region.add(circle(20, 280, 430));
            region.add(new int[] { 20, 0, 280, 0, 280, 450, 20, 450 });
            region.add(new int[] { 0, 20, 300, 20, 300, 430, 0, 430 });
            shell.setRegion(region);
            Rectangle size = region.getBounds();
            shell.setSize(size.width, size.height);
            resize.setBounds(140, 430, 20, 20);
            isMaximized = false;
        }
    }
});

isMaximized 是一个布尔值,用于了解 shell 是否处于 "big" 形状。 这将可能的大小限制为两个,但对我来说已经足够了。