Java SWT-具有自定义形状的可调整大小的外壳
我有一个应用程序,你可以在其中调用带有控件的小窗口。我希望这些小窗户是长方形的,有圆角,没有任何装饰。我发现了这个老问题并使用了它: 我的结局是:Java SWT-具有自定义形状的可调整大小的外壳,java,eclipse,resize,swt,Java,Eclipse,Resize,Swt,我有一个应用程序,你可以在其中调用带有控件的小窗口。我希望这些小窗户是长方形的,有圆角,没有任何装饰。我发现了这个老问题并使用了它: 我的结局是: 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.gra
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();
}
}
包装swt\u窗口测试;
导入org.eclipse.swt.swt;
导入org.eclipse.swt.events.SelectionEvent;
导入org.eclipse.swt.events.SelectionListener;
导入org.eclipse.swt.graphics.Point;
导入org.eclipse.swt.graphics.Rectangle;
导入org.eclipse.swt.graphics.Region;
导入org.eclipse.swt.widgets.Button;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Event;
导入org.eclipse.swt.widgets.Listener;
导入org.eclipse.swt.widgets.Shell;
公共类SingleDisplayMultipleShell
{
//圈
静态int[]圆(int r,int offsetX,int offsetY){
int[]多边形=新的int[8*r+4];
//x^2+y^2=r^2
对于(int i=0;i<2*r+1;i++){
int x=i-r;
inty=(int)Math.sqrt(r*r-x*x);
多边形[2*i]=偏移量x+x;
多边形[2*i+1]=offsetY+y;
多边形[8*r-2*i-2]=偏移量x+x;
多边形[8*r-2*i-1]=偏移量-y;
}
返回多边形;
}
// ========================================
//表示子Shell的内部类
// ========================================
私有类儿童壳
{
公共子Shell(Shell主窗口Shell)
{
System.out.println(“创建新的子Shell”);
Display Display=mainWindowShell.getDisplay();
// =========================================
//从显示器创建外壳(窗口)
// =========================================
最终外壳=新外壳(主窗框,SWT.NO|U装饰| SWT.ON|U顶部);
shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
//定义一个看起来像钥匙孔的区域
区域=新区域();
添加(圈(10,10,10));
添加(圈(10,10500));
添加(圈(10500,10));
添加(圆圈(1050500));
添加(新的int[]{10,0,10,510,500,510,500,0});
添加(新的int[]{0,10,0,500,510,500,510,10});
//使用setRegion定义外壳的形状
壳.setRegion(region);
矩形大小=region.getBounds();
外壳尺寸(尺寸、宽度、尺寸、高度);
//添加移动shell的能力
侦听器l=新侦听器(){
点源;
公共无效handleEvent(事件e){
开关(e型){
案例SWT.MouseDown:
原点=新点(e.x,e.y);
打破
案例SWT.MouseUp:
原点=空;
打破
case SWT.MouseMove:
如果(原点!=null){
点p=display.map(shell,null,e.x,e.y);
外壳设置位置(p.x-原点.x,p.y-原点.y);
}
打破
}
}
};
shell.addListener(SWT.MouseDown,l);
shell.addListener(SWT.MouseUp,l);
shell.addListener(SWT.MouseMove,l);
//添加关闭shell的功能
按钮b=新按钮(外壳,SWT.按钮);
b、 setBackground(shell.getBackground());
b、 setText(“关闭”);
b、 包装();
b、 设定位置(10,68);
b、 addListener(SWT.Selection,newlistener()){
公共无效handleEvent(事件e){
shell.close();
}
});
shell.open();
// =============================================================
//在子Shell上注册关闭事件的侦听器。
//这将处理孩子的外壳
// =============================================================
addListener(SWT.Close,new Listener())
{
@凌驾
公共无效handleEvent(事件)
{
System.out.println(“子Shell处理关闭事件,即将处理此Shell”);
shell.dispose();
}
});
}
}
公共单显示多网格()
{
// ======================================================
//创建表示UI的主显示对象
//子系统,并包含单个UI处理线程
// ======================================================
最终显示=Display.getDefault();
// ====================================================
//从显示屏为主窗口创建外壳
// ====================================================
最终外壳主窗口外壳=新外壳(显示,SWT.CLOSE);
// =====================
//设置窗口标题
// =====================
mainWindowsShell.setText(“主Shell”);
// =========================================
//创建生成子shell的按钮
// =========================================
Button spawn=新按钮(MainWindowsShell,SWT.PUSH);
spawn.setText(“创建子对象”);
产卵后缘(10,10,150,30);
spawn.addSelectionListener(新SelectionListener()
{
@凌驾
公共无效WidgeSelected(SelectionEvent e)
{
// =====================================================
//按下按钮时,创建一个子壳对象
//主显示器。孩子还可以访问
//通过调用display.getDefault()显示自身
// =====================================================
System.out.println(“按下主Shell处理按钮,即将创建子Shell”);
新的ChildShell(MainWindowsShell);
}
@凌驾
公共无效widgetDefaultSelected(SelectionEvent e)
{
已选定(e);
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;
}
}
});