在ScrolledComposite中有两个复合体(Having Two Composites inside of a ScrolledComposite)

编程入门 行业动态 更新时间:2024-10-08 22:47:28
在ScrolledComposite中有两个复合体(Having Two Composites inside of a ScrolledComposite)

我正在尝试创建一个图例,我想要的方式是有两个单独的复合体,一个在scrollledComposite内部的另一个上面。 顶级复合子元素是标签和画布的另一种组合。 此复合从属性文件中填充。 第二个复合材料与第一个复合材料相同,其中儿童是同一种复合材料。 我怎样才能实现这一目标。 以下代码的作用是在读取文件时,它会生成新的复合并填充主复合。 我希望最后三个图例项与第一个图例分开,并作为第一个复合项。

@Override public void createPartControl(Composite parent) { display = parent.getDisplay(); parent.setLayout(new FillLayout()); // This enables the display to be able to be scrollable when needed scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setExpandVertical(true); // making a new composite inside of the scrolledComposite to be to add labels and canvases stepComposite = new Composite(scrolledComposite, SWT.NONE); blockedComposite = new Composite(scrolledComposite, SWT.NONE); // making the layout for the composite a row so when the the children reach the end of the shell // the children will wrap down to the next level RowLayout layout = new RowLayout(); layout.pack = false; layout.spacing = 5; stepComposite.setLayout(layout); blockedComposite.setLayout(layout); } /** * Adding a new composite for a rectangle and label to be added to the legend * @param legendMessage */ public static void addNewComposite(String legendMessage, int compositePosition) { final String message = legendMessage; final String[] s = message.split(","); final int position = compositePosition; if (display != null) { display.syncExec(new Runnable() { @Override public void run() { Composite com; if (position == 1) { com = new Composite(blockedComposite, SWT.NONE); } else { com = new Composite(stepComposite, SWT.NONE); } // Composite com = new Composite(stepComposite, SWT.NONE); com.setLayout(new GridLayout(2, false)); // Creating the color using the RBG values final Color color = new Color(display, Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2])); // Creating a canvas for which the rectangle can be drawn on Canvas canvas = new Canvas(com, SWT.NONE); canvas.setLayoutData(new GridData(50,40)); // TODO Maybe set the bounds of the canvas canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawRectangle(1, 1, 48, 38); e.gc.setBackground(color); e.gc.fillRectangle(2, 2, 47, 37); } }); // Creating a label and setting the font Label label = new Label(com, SWT.NULL); final Font boldFont = new Font(label.getDisplay(), new FontData("Arial", 12, SWT.BOLD)); label.setFont(boldFont); label.setText(s[3]); // Adding a color and font disposer to the composite com.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { color.dispose(); boldFont.dispose(); } }); // Adding the composite into a map to be able to be deleted when model stops comps.put(s[3], com); if (position == 1) { // scrolledComposite.setContent(blockedComposite); // scrolledComposite.setMinSize(blockedComposite.computeSize(1, 1000)); } else { scrolledComposite.setContent(stepComposite); scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000)); } // Adding the composite to the scrolledComposite to be able to appear // scrolledComposite.setContent(stepComposite); // Setting the size of the scrolledComposite // scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000)); } }); } }

这段代码用第二个复合替换了第一个复合材料,我很欣赏一些新的眼睛来帮助我找出问题所在。

I am trying to create a legend and the way I would like to do it is have two separate composites, one on top of another inside of a scrolledComposite. the top composite children is another composite of a label and a canvas. This composite is filled from a properties file. The second composite is the same as the first with the children being the same kind of composites. How would I be able to achieve that. What the code below does is it while the file is being read, it makes the new composite and fills the main composite. I want the last three legend items be separate from the first ones and act as the first composite.

@Override public void createPartControl(Composite parent) { display = parent.getDisplay(); parent.setLayout(new FillLayout()); // This enables the display to be able to be scrollable when needed scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setExpandVertical(true); // making a new composite inside of the scrolledComposite to be to add labels and canvases stepComposite = new Composite(scrolledComposite, SWT.NONE); blockedComposite = new Composite(scrolledComposite, SWT.NONE); // making the layout for the composite a row so when the the children reach the end of the shell // the children will wrap down to the next level RowLayout layout = new RowLayout(); layout.pack = false; layout.spacing = 5; stepComposite.setLayout(layout); blockedComposite.setLayout(layout); } /** * Adding a new composite for a rectangle and label to be added to the legend * @param legendMessage */ public static void addNewComposite(String legendMessage, int compositePosition) { final String message = legendMessage; final String[] s = message.split(","); final int position = compositePosition; if (display != null) { display.syncExec(new Runnable() { @Override public void run() { Composite com; if (position == 1) { com = new Composite(blockedComposite, SWT.NONE); } else { com = new Composite(stepComposite, SWT.NONE); } // Composite com = new Composite(stepComposite, SWT.NONE); com.setLayout(new GridLayout(2, false)); // Creating the color using the RBG values final Color color = new Color(display, Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2])); // Creating a canvas for which the rectangle can be drawn on Canvas canvas = new Canvas(com, SWT.NONE); canvas.setLayoutData(new GridData(50,40)); // TODO Maybe set the bounds of the canvas canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawRectangle(1, 1, 48, 38); e.gc.setBackground(color); e.gc.fillRectangle(2, 2, 47, 37); } }); // Creating a label and setting the font Label label = new Label(com, SWT.NULL); final Font boldFont = new Font(label.getDisplay(), new FontData("Arial", 12, SWT.BOLD)); label.setFont(boldFont); label.setText(s[3]); // Adding a color and font disposer to the composite com.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { color.dispose(); boldFont.dispose(); } }); // Adding the composite into a map to be able to be deleted when model stops comps.put(s[3], com); if (position == 1) { // scrolledComposite.setContent(blockedComposite); // scrolledComposite.setMinSize(blockedComposite.computeSize(1, 1000)); } else { scrolledComposite.setContent(stepComposite); scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000)); } // Adding the composite to the scrolledComposite to be able to appear // scrolledComposite.setContent(stepComposite); // Setting the size of the scrolledComposite // scrolledComposite.setMinSize(stepComposite.computeSize(1, 1000)); } }); } }

this code replaces the first composite with the second composite and I would appreciate some new eyes to help me figure out the problem.

最满意答案

ScrolledComposite仅显示您在setContent指定的Composite ,您不能在该级别拥有多个复合。

使用一个Composite作为ScrolledComposite的直接子项,并将其他组合作为该单个组合的子项添加。 不要忘记在所有复合材料上设置布局。

ScrolledComposite only shows the Composite which you specify on setContent, you can't have multiple composites at that level.

Use one Composite as the direct child of ScrolledComposite and add your other composites as children of that single composite. Don't forget to set a Layout on all the composites.

更多推荐

本文发布于:2023-08-04 00:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复合体   中有   两个   ScrolledComposite   Composites

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!