我实现了一个带有3个窗格的Java SWT SashForm:
SashForm oSash = new SashForm(cmptParent, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
oSash.setLayout(gridLayout);
oSash.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
Composite oPaneLeft = new Composite(oSash, SWT.NONE);
...
Composite oPaneMiddle = new Composite(oSash, SWT.NONE);
...
Composite oPaneRight = new Composite(oSash, SWT.NONE);
这个想法是有一个固定大小的中间分区。设置初始宽度很简单。
我希望能够通过拖动中间来调整表单大小。用户点击中间并向左或向右拖动,从而保持中间窗格固定,只需向左或向右滑动。我可以按如下方式实现此功能:
private static Boolean sisResizeSashMiddle = false;
private static int siPosSashMiddleOffset = 0;
...
cmptPaneMiddle = new Composite(cmptParent, SWT.NONE);
cmptPaneMiddle.addMouseListener(new MouseAdapter()
{
@Override
public void mouseDown(MouseEvent arg0)
{
// The user wishes to resize the sash.
AppMain.sisResizeSashMiddle = true;
AppMain.siPosSashMiddleOffset = arg0.x - AppMain.siPosSashMiddleStart;
}
@Override
public void mouseUp(MouseEvent arg0)
{
// The user finished resizing the sash.
AppMain.sisResizeSashMiddle = false;
}
});
cmptPaneMiddle.addMouseMoveListener(new MouseMoveListener()
{
public void mouseMove(MouseEvent arg0)
{
// Only resize the sashes if user hold down the mouse while dragging.
if (true == AppMain.sisResizeSashMiddle)
{
// Compute the width of each sash.
int icxShell = shell.getSize().x;
int icxLeft = arg0.x - AppMain.siPosSashMiddleOffset;
int icxMiddle = AppMain.BrowserSash_Pane_Middle_Width;
int icxRight = shell.getSize().x - icxLeft - icxMiddle;
// Compute the weights.
int iWeightLeft = 10000 * icxLeft / icxShell;
int iWeightMiddle = 10000 * icxMiddle / icxShell;
int iWeightRight = 10000 * icxRight / icxShell;
// Set the weights.
int[] weights = new int[] {iWeightLeft, iWeightMiddle, iWeightRight};
oSash.setWeights(weights);
}
}
});
我的问题是滑动实现是生涩和紧张,绝对不顺利。有没有更好的方法来获得相同的效果,只是平滑而没有生涩的行为?
答案 0 :(得分:1)
尝试使用SWT.SMOOTH
上的SashForm
标记:
SashForm oSash = new SashForm(cmptParent, SWT.SMOOTH);