0
我試圖在畫布上顯示比窗口/畫布的大小限制允許的更多。我知道這有幾個方法。我可以使用FigureCanvas,但我不確定視口如何與它結合。我選擇創建一個ScrolledComposite並在其中放置一個畫布。我的問題是滾動條出現,但移動它們對畫布上顯示的內容沒有任何影響。任何人都可以請指出我正確的方向來解決這個問題嗎?謝謝。 *我添加了簡短的可測試代碼來說明問題。啓用畫布SWT的滾動Draw2d
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.SimpleRaisedBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.xml.sax.SAXException;
import org.eclipse.swt.custom.ScrolledComposite;
public class ScrollDemo extends Dialog {
protected Shell shell;
protected IFigure panel;
protected IFigure v_panel;
public static void main(String[] args)
{
ScrollDemo act= new ScrollDemo(new Shell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
act.open();
}
/**
* Create the dialog.
* @param parent
* @param style
*/
public ScrollDemo(Shell parent, int style) {
super(parent, style);
setText("Scrolling Demo");
}
/**
* Open the dialog.
* @throws SAXException
*/
public void open(){
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the dialog.
*/
private void createContents(){
shell = new Shell(getParent(), getStyle());
shell.setSize(475, 375);
shell.setText(getText());
shell.setLayout(new GridLayout(1, false));
ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
GridData gd_scrolledComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_scrolledComposite.widthHint = 453;
gd_scrolledComposite.heightHint = 245;
scrolledComposite.setLayoutData(gd_scrolledComposite);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
Canvas canvas = new Canvas(scrolledComposite, SWT.H_SCROLL | SWT.V_SCROLL);
canvas.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_YELLOW));
scrolledComposite.setContent(canvas);
scrolledComposite.setMinSize(canvas.computeSize(SWT.DEFAULT, SWT.DEFAULT));
LightweightSystem lws = new LightweightSystem(canvas);
panel = new Figure();
lws.setContents(panel);
int starting_xpos= 0;
int starting_ypos= 0;
makeStructure(starting_xpos, starting_ypos, 0);
}
private void makeStructure(int starting_xpos, int starting_ypos, int index)
{
org.eclipse.draw2d.Label lblRoot= new org.eclipse.draw2d.Label();
Rectangle rect= new Rectangle(starting_xpos, starting_ypos, 200, 25);
//Anything longer than 200 cannot be displayed
lblRoot.setText("BAAAAAAAAAAAB");
lblRoot.setBorder(new SimpleRaisedBorder());
lblRoot.setForegroundColor(ColorConstants.black);
lblRoot.setVisible(true);
lblRoot.setOpaque(true);
lblRoot.setBounds(rect);
panel.add(lblRoot);
if (index<10)
{
makeStructure(starting_xpos+20, starting_ypos+50, index+1);
}
}
}
非常感謝。如何增加可通過滾動訪問的畫布大小?改變畫布/視口的邊界對我沒有任何幫助。 – Asher
@Asher你是否需要增加它,因爲你添加了新的數字,他們超出了界限,或者你只是想增加它? – Baz
我已經有了這兩種情況下的需求...... – Asher