gpt4 book ai didi

java - SWT Sash/SashForm 镜像

转载 作者:行者123 更新时间:2023-11-29 05:22:47 25 4
gpt4 key购买 nike

我尝试在水平 SashForm(父级)中实现上下 SashForm(子级 A 和 B)。我希望能够拖动其中一个 child ( child A)的(垂直)腰带,并让另一个 child ( child B)的(垂直)腰带完全模仿 A 的水平位置。为此,我尝试了一个 hack,创建了一个公开 SashForms 窗扇的“ModifiedSashForm”:

public Sash[] getSashes() {
return sashes;

然后我创建了一个扩展 ModifiedSashForm 并覆盖的类

public void onDragSash(Event event){

...

向听众(例如 child A)发送 B 的腰带已被拖动的注释: if(sash.getParent().getData(WidgetData.DATA_DRAG_SOURCE).equals(WidgetData.DRAG_SOURCE.SOURCE)){ fireSashDragged(事件,sashIndex); }}

然后 child A 将 B 的窗框替换为事件源(!):

if(sashes[sashIndex]!=null){
event.widget=sashes[sashIndex];
}

listener.onDragSash(event);

然后我得到了一个可以正常运行的图形用户界面。但是我遇到了窗扇不同步的问题。代码很糟糕,我不想以这种方式破坏 SWT。然后我尝试获取 child B 的权重并将它们插入 A - 但再次遇到问题 - 特别是当窗框被拖到彼此靠近时,听者的权重顺序翻转。

接下来我尝试使用 Sash 和 FormLayout。示例代码如下 - 但我仍然无法让上部垂直窗扇准确模仿下部垂直窗扇的运动,反之亦然。

//基于http://www.eclipse.org/swt/snippets/ SWT Snippet107

public class SashTest {
final private List<Sash> listeners = new ArrayList<Sash>();
private Shell shell;

public static void main(String[] args) {
new SashTest().run();

}

void run() {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("SashTest");
this.shell = shell;
init();

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

void init() {

final FormLayout form = new FormLayout();
shell.setLayout(form);

SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
sashForm.setLayoutData(new FormData());
Composite comp1 = new Composite(sashForm, SWT.NONE);
comp1.setLayout(new FormLayout());
createTopArea(comp1);

Composite comp2 = new Composite(sashForm, SWT.NONE);
comp2.setLayout(new FormLayout());
createBottomArea(comp2);

}

void createTopArea(Composite comp) {
Button button1 = new Button(comp, SWT.PUSH);
FormData fd_button1 = new FormData();
fd_button1.bottom = new FormAttachment(0);
fd_button1.right = new FormAttachment(0);
fd_button1.top = new FormAttachment(0);
fd_button1.left = new FormAttachment(0);
button1.setLayoutData(fd_button1);
button1.setText("Button 1");

final Sash sash = new Sash(comp, SWT.VERTICAL);
FormData fd_sash = new FormData();
fd_sash.bottom = new FormAttachment(0);
fd_sash.right = new FormAttachment(0);
fd_sash.top = new FormAttachment(0);
fd_sash.left = new FormAttachment(0);
sash.setLayoutData(fd_sash);
//add the upper sash to listeners for bottom sash movement
listeners.add(sash);

Button button2 = new Button(comp, SWT.PUSH);
FormData fd_button2 = new FormData();
fd_button2.bottom = new FormAttachment(0);
fd_button2.right = new FormAttachment(0);
fd_button2.top = new FormAttachment(0);
fd_button2.left = new FormAttachment(0);
button2.setLayoutData(fd_button2);
button2.setText("Button 2");

FormData button1Data = new FormData();
button1Data.left = new FormAttachment(0, 0);
button1Data.right = new FormAttachment(sash, 0);
button1Data.top = new FormAttachment(0, 0);
button1Data.bottom = new FormAttachment(100, 0);
button1.setLayoutData(button1Data);

final int limit = 20, percent = 50;
final FormData sashData = new FormData();
sashData.left = new FormAttachment(percent, 0);
sashData.top = new FormAttachment(0, 0);
sashData.bottom = new FormAttachment(100, 0);
sash.setLayoutData(sashData);
sash.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
Rectangle sashRect = sash.getBounds();
Rectangle shellRect = shell.getClientArea();
int right = shellRect.width - sashRect.width - limit;
e.x = Math.max(Math.min(e.x, right), limit);
if (e.x != sashRect.x) {
sashData.left = new FormAttachment(0, e.x);
shell.layout();
notifyListeners(sashData);
}
}
});

FormData button2Data = new FormData();
button2Data.left = new FormAttachment(sash, 0);
button2Data.right = new FormAttachment(100, 0);
button2Data.top = new FormAttachment(0, 0);
button2Data.bottom = new FormAttachment(100, 0);
button2.setLayoutData(button2Data);

}

void createBottomArea(Composite comp) {
Button button3 = new Button(comp, SWT.PUSH);
FormData fd_button1 = new FormData();
fd_button1.bottom = new FormAttachment(0);
fd_button1.right = new FormAttachment(0);
fd_button1.top = new FormAttachment(0);
fd_button1.left = new FormAttachment(0);
button3.setLayoutData(fd_button1);
button3.setText("Button 3");

final Sash sash2 = new Sash(comp, SWT.VERTICAL);
FormData fd_sash2 = new FormData();
fd_sash2.bottom = new FormAttachment(0);
fd_sash2.right = new FormAttachment(0);
fd_sash2.top = new FormAttachment(0);
fd_sash2.left = new FormAttachment(0);
sash2.setLayoutData(fd_sash2);

Button button4 = new Button(comp, SWT.PUSH);
FormData fd_button2 = new FormData();
fd_button2.bottom = new FormAttachment(0);
fd_button2.right = new FormAttachment(0);
fd_button2.top = new FormAttachment(0);
fd_button2.left = new FormAttachment(0);
button4.setLayoutData(fd_button2);
button4.setText("Button 4");

FormData button3Data = new FormData();
button3Data.left = new FormAttachment(0, 0);
button3Data.right = new FormAttachment(sash2, 0);
button3Data.top = new FormAttachment(0, 0);
button3Data.bottom = new FormAttachment(100, 0);
button3.setLayoutData(button3Data);

final int limit = 20, percent = 50;
final FormData sashData2 = new FormData();
sashData2.left = new FormAttachment(percent, 0);
sashData2.top = new FormAttachment(0, 0);
sashData2.bottom = new FormAttachment(100, 0);
sash2.setLayoutData(sashData2);
sash2.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
Rectangle sashRect = sash2.getBounds();
Rectangle shellRect = shell.getClientArea();
int right = shellRect.width - sashRect.width - limit;
e.x = Math.max(Math.min(e.x, right), limit);
if (e.x != sashRect.x) {
sashData2.left = new FormAttachment(0, e.x);
shell.layout();
}
}
});

FormData button4Data = new FormData();
button4Data.left = new FormAttachment(sash2, 0);
button4Data.right = new FormAttachment(100, 0);
button4Data.top = new FormAttachment(0, 0);
button4Data.bottom = new FormAttachment(100, 0);
button4.setLayoutData(button4Data);

}

void notifyListeners(FormData formData){
for(Sash sash: listeners){
Rectangle sashRect = sash.getBounds();

FormData sashData = formData;
sash.setLayoutData(sashData);
shell.layout();
}

}

我是否必须编写自己的小部件才能执行此操作,或者可以使用 Sash/SashForm 实现?

Baz,我在这里编辑了你的类(class)(太大而无法评论):

public class SashMirror {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setMinimumSize(new Point(200, 200));
shell.setText("StackOverflow");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
final SashForm divider = new SashForm(shell, SWT.VERTICAL);
divider.setLayout(new FillLayout(SWT.HORIZONTAL));

final SashForm top = new SashForm(divider, SWT.HORIZONTAL);
top.setLayout(new FillLayout(SWT.HORIZONTAL));

final SashForm bottom = new SashForm(divider, SWT.HORIZONTAL);
bottom.setLayout(new FillLayout(SWT.HORIZONTAL));

Canvas canvasTopLeft = new Canvas(top, SWT.NONE);
canvasTopLeft
.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
canvasTopLeft.setLayout(new FillLayout(SWT.HORIZONTAL));

Canvas canvasTopMiddle = new Canvas(top, SWT.NONE);
canvasTopMiddle.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WHITE));
canvasTopMiddle.setLayout(new FillLayout(SWT.HORIZONTAL));

Canvas canvasTopRight = new Canvas(top, SWT.NONE);
canvasTopRight.setBackground(SWTResourceManager
.getColor(SWT.COLOR_BLUE));
canvasTopRight.setLayout(new FillLayout(SWT.HORIZONTAL));

Canvas canvasBottomLeft = new Canvas(bottom, SWT.NONE);
canvasBottomLeft.setBackground(SWTResourceManager
.getColor(SWT.COLOR_DARK_YELLOW));
canvasBottomLeft.setLayout(new FillLayout(SWT.HORIZONTAL));

Canvas canvasBottomMiddle = new Canvas(bottom, SWT.NONE);
canvasBottomMiddle.setBackground(SWTResourceManager
.getColor(SWT.COLOR_DARK_RED));
canvasBottomMiddle.setLayout(new FillLayout(SWT.HORIZONTAL));

Canvas canvasBottomRight = new Canvas(bottom, SWT.NONE);
canvasBottomRight.setBackground(SWTResourceManager
.getColor(SWT.COLOR_DARK_GREEN));
canvasBottomRight.setLayout(new FillLayout(SWT.HORIZONTAL));

top.setWeights(new int[] { 2, 2, 2 });
bottom.setWeights(new int[] { 2, 2, 2 });

canvasBottomLeft.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event arg0) {
top.setWeights(bottom.getWeights());
}
});

canvasTopLeft.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event arg0) {
bottom.setWeights(top.getWeights());
}
});

shell.pack();
shell.open();

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

最佳答案

这完美地工作:

public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));

final SashForm top = new SashForm(shell, SWT.HORIZONTAL);
top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final SashForm bottom = new SashForm(shell, SWT.HORIZONTAL);
bottom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Button topLeft = new Button(top, SWT.PUSH);
topLeft.setText("Top left");
new Button(top, SWT.PUSH).setText("Top right");
Button bottomLeft = new Button(bottom, SWT.PUSH);
bottomLeft.setText("Bottom left");
new Button(bottom, SWT.PUSH).setText("Bottom right");

topLeft.addListener(SWT.Resize, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
bottom.setWeights(top.getWeights());
}
});

bottomLeft.addListener(SWT.Resize, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
top.setWeights(bottom.getWeights());
}
});

top.setWeights(new int[] {1,2});
bottom.setWeights(new int[] {1,2});

shell.pack();
shell.open();

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

调整大小之前:

enter image description here

调整大小后:

enter image description here

更新:

我注意到您实际调整大小的 SashForm 最终会不同步。我通过将 Listener 更改为:

“修复”了这个问题
topLeft.addListener(SWT.Resize, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
bottom.setWeights(top.getWeights());
// Resize the source SashForm as well to fix out of sync!
top.setWeights(bottom.setWeights());
}
});

关于java - SWT Sash/SashForm 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015489/

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