【问题标题】:Enabling scrolling for canvas SWT Draw2d为画布 SWT Draw2d 启用滚动
【发布时间】:2014-07-27 12:31:58
【问题描述】:

我试图在画布上显示超出窗口/画布大小限制所允许的更多内容。我知道有几种方法可以做到这一点。我可以使用 FigureCanvas 但我不确定视口如何与它联系起来。我选择创建一个 ScrolledComposite 并在其中放置一个画布。我的问题是出现了滚动条,但移动它们对画布上显示的内容没有影响。谁能指出我解决这个问题的正确方向?谢谢。 *我添加了简短的可测试代码来说明问题。

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);
            }
        }


    }

【问题讨论】:

    标签: java user-interface swt draw2d


    【解决方案1】:

    好的,我设法通过使用 FigureCanvas 而不是 Canvas 并使用 Viewport 作为根元素来使其工作:

    public class Draw2d extends Dialog
    {
        protected Shell     shell;
        private Viewport    viewport;
    
        public static void main(String[] args)
        {
            Draw2d act = new Draw2d(new Shell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
            act.open();
        }
    
        public Draw2d(Shell parent, int style)
        {
            super(parent, style);
            setText("Draw2d");
        }
    
        public void open()
        {
            createContents();
            shell.open();
            shell.layout();
            Display display = getParent().getDisplay();
            while (!shell.isDisposed())
            {
                if (!display.readAndDispatch())
                {
                    display.sleep();
                }
            }
        }
    
        private void createContents()
        {
            shell = new Shell(getParent(), getStyle());
            shell.setSize(475, 375);
            shell.setText(getText());
            shell.setLayout(new GridLayout(1, false));
    
            FigureCanvas canvas = new FigureCanvas(shell, SWT.H_SCROLL | SWT.V_SCROLL);
            canvas.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_YELLOW));
            canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            canvas.setScrollBarVisibility(FigureCanvas.ALWAYS);
            viewport = new Viewport(true);
            canvas.setViewport(viewport);
    
            LightweightSystem lws = new LightweightSystem(canvas);
            lws.setContents(viewport);
    
            makeStructure(0, 0, 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, 210, 25);
    
            lblRoot.setText("BAAAAAAAAAAAB");
    
            lblRoot.setBorder(new SimpleRaisedBorder());
            lblRoot.setForegroundColor(ColorConstants.black);
            lblRoot.setVisible(true);
            lblRoot.setOpaque(true);
            lblRoot.setBounds(rect);
    
            viewport.add(lblRoot);
    
            if (index < 10)
            {
                makeStructure(starting_xpos + 20, starting_ypos + 50, index + 1);
            }
        }
    }
    

    现在滚动效果很好。

    【讨论】:

    • 非常感谢。如何增加可以通过滚动访问的画布的大小?更改画布/视口的边界对我没有任何帮助。
    • @Asher 您是否需要增加它,因为您添加了新数字并且它们超出了界限,或者您只是想增加它?
    • 开箱即用是什么意思?目前,它可以很好地滚动,但只能滚动到设定的宽度和高度。如何更改这些值?
    • @Asher 所以当你添加新人物时,它不会调整它的边界?
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多