gpt4 book ai didi

java - SWT应用程序: Are Draggable Tabs Possible?

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:48 26 4
gpt4 key购买 nike

我正在使用 SWT 构建 Java 应用程序。应用程序的要求之一是它具有多个窗口。我认为实现像大多数浏览器中那样的功能会很酷,而不是拥有“永远独立”的窗口,在大多数浏览器中,您有一个单一的表格窗口,其中每个选项卡都可以拖出以创建一个单独的窗口。使用 Google 进行一些研究后,似乎可以实现此目的 using JavaFX ,但是是否有可能(并且相对容易)在 SWT 中实现相同的功能?提前致谢。

最佳答案

虽然我的看法可能有点晚了,但它仍然是这样。下面的代码片段是一个粗略的 POC,它允许从 CTabFolder 中拖动项目,当该项目被拖放到文件夹的边界之外时,会打开一个 shell 以显示项目的内容。

public static void main( String[] args ) {
Display display = new Display();
final Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
final CTabFolder folder = new CTabFolder( shell, SWT.BORDER );
for( int i = 0; i < 4; i++ ) {
CTabItem item = new CTabItem( folder, SWT.CLOSE );
item.setText( "Item " + i );
Text text = new Text( folder, SWT.MULTI );
text.setText( "Content for Item " + i );
item.setControl( text );
}
Listener dragListener = new Listener() {
private CTabItem dragItem;

public void handleEvent( Event event ) {
Point mouseLocation = new Point( event.x, event.y );
switch( event.type ) {
case SWT.DragDetect: {
CTabItem item = folder.getItem( mouseLocation );
if( dragItem == null && item != null ) {
dragItem = item;
folder.setCapture( true );
}
break;
}
case SWT.MouseUp: {
if( dragItem != null && !folder.getBounds().contains( mouseLocation ) ) {
popOut( dragItem, folder.toDisplay( mouseLocation ) );
dragItem.dispose();
dragItem = null;
}
break;
}
}
}
};
folder.addListener( SWT.DragDetect, dragListener );
folder.addListener( SWT.MouseUp, dragListener );
shell.pack();
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}

private static void popOut( CTabItem tabItem, Point location ) {
Control control = tabItem.getControl();
tabItem.setControl( null );
Shell itemShell = new Shell( tabItem.getParent().getShell(), SWT.DIALOG_TRIM | SWT.RESIZE );
itemShell.setLayout( new FillLayout() );
control.setParent( itemShell );
control.setVisible( true ); // control is hidden by tabItem.setControl( null ), make visible again
itemShell.pack();
itemShell.setLocation( location );
itemShell.open();
}

虽然此示例使用 CTabFolder,但也应该可以使用( native )TabFolder

肯定缺少的是拖动项目时的视觉反馈和中止拖动操作的方法(例如 Esc 键),可能还有更多东西......

关于java - SWT应用程序: Are Draggable Tabs Possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29738289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com