【问题标题】:Use GridLayout in Scrolled Composite在滚动复合中使用 GridLayout
【发布时间】:2012-04-26 17:03:52
【问题描述】:

我正在尝试创建一个滚动合成,其中包含一个内部带有网格布局的合成。

但是,当我尝试设置滚动合成的内容时,什么都没有加载。 这似乎只会影响具有网格布局的复合材料。

我做错了什么?

我的代码:

    CTabItem tbtmNotes = new CTabItem(tabFolder, SWT.NONE);
    tbtmNotes.setText("Notes");
    ScrolledComposite scrollComposite = new ScrolledComposite(tabFolder, SWT.V_SCROLL | SWT.BORDER);
    tbtmNotes.setControl(scrollComposite);
    scrollComposite.setContent(new hm_Composites.Comp_Animal_Notes(tabFolder, SWT.None, a));

【问题讨论】:

    标签: java swt composite scrolledcomposite


    【解决方案1】:

    经过一番摆弄,我得到了以下工作,希望它可以帮助别人。

        public Comp_Animal_Notes(Composite parent, int style, Animal a)
            throws Exception {
        super(parent, SWT.NONE);
        setLayout(new FillLayout(SWT.HORIZONTAL));
        ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
    
        Composite composite = new Composite(scrolledComposite, SWT.NONE);
        composite.setLayout(new GridLayout(2, false));
    
        ArrayList<Note> alNotes = a.getAnimalNotes();
        this.setRedraw(false);
        for (int i = 0; i < alNotes.size(); i++) {
            Note note = alNotes.get(i);
    
            CLabel lblNoteDate = new CLabel(composite, SWT.BORDER);
            lblNoteDate.setFont(SWTResourceManager.getFont("Tahoma", 8,
                    SWT.BOLD));
            lblNoteDate.setText("Date:");
            lblNoteDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    
            CLabel lblNotedatetxt = new CLabel(composite, SWT.BORDER);
            lblNotedatetxt.setBackground(SWTResourceManager
                    .getColor(SWT.COLOR_WHITE));
            lblNotedatetxt.setText(note.getUserDate());
            lblNotedatetxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    
            CLabel lblNoteTxt = new CLabel(composite, SWT.BORDER);
            lblNoteTxt.setBackground(SWTResourceManager
                    .getColor(SWT.COLOR_WHITE));
            lblNoteTxt.setText(note.getStrNote());
            lblNoteTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
        }
    
        scrolledComposite.setContent(composite);
        scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
        this.setRedraw(true);
    }
    

    【讨论】:

    • 确实很有帮助!感谢分享!
    【解决方案2】:

    如果来自 Talon06 的 sn-p 不起作用,请尝试进行以下更改。

    scrolledComposite.setContent(composite);
    
    //add this line
    composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
    //not sure about this line, was optional in my case
    scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
    this.setRedraw(true);
    

    我发现 ScrolledComposite 中的组件只有在我明确设置复合材料的大小时才会变得可见。

    @Talon06:感谢您的精彩发现!

    【讨论】:

    • 感谢您所做的更改,这使得复合材料在 Windows Builder 设计选项卡中可见,这让我让事情变得更快。
    猜你喜欢
    • 2022-08-13
    • 1970-01-01
    • 2019-02-14
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多