0

我正在创建一个图例视图,外壳内部应该有一个矩形,后跟一个描述颜色的标签。我能够使用普通合成来使视图工作,但图例继续超出屏幕,如果不放大窗口就无法看到它。我正在尝试为我的 shell 使用 scrolledComposite 视图,但是当我执行程序时,什么也没有出现。

  public void createPartControl(Composite parent)
{
  display = parent.getDisplay();
  parent.setLayout(new FillLayout());
  sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);

  LegendView.composite = new Composite(sc, SWT.NONE);
  RowLayout layout = new RowLayout();
  layout.wrap = true;
  layout.spacing = 5;
  composite.setLayout(layout);

}

public static void addRectangle(String legendMessage)
{
  final String propId = legendMessage;
  final String[] s = propId.split(",");
  if (display != null)
  {
     display.syncExec(new Runnable()
     {
        @Override
        public void run()
        {
           // 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(composite, SWT.NONE);
           // Maybe set the bounds of the canvas
           canvas.addPaintListener(new PaintListener()
           {
              public void paintControl(PaintEvent e)
              {
                 e.gc.drawRectangle(1, 1, 50, 60);
                 e.gc.setBackground(color);
                 e.gc.fillRectangle(2, 2, 49, 59);
              }
           });
           // Disposing the color after it has been used
           canvas.addDisposeListener(new DisposeListener()
           {
              public void widgetDisposed(DisposeEvent e)
              {
                 color.dispose();
              }
           });
           // Creating a label and setting the font
           Label label = new Label(composite, SWT.NULL);
           Font boldFont = new Font( label.getDisplay(), new FontData( "Arial", 12, SWT.BOLD ) ); 
           label.setFont( boldFont ); 

           label.setText(s[3]);              

           composite.redraw();
           composite.layout(true);
           sc.setContent(composite);
        }
     });
  }
}

我在不同的类中调用添加矩形。我在使用 SWT 方面相当陌生,在查看示例并阅读了滚动合成的文档之后,我将其解释为。任何帮助将不胜感激。

4

1 回答 1

2

您还没有告诉ScrolledComposite如何管理大小。您必须调用setSizesetMinSize。为此,您可能想要:

sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
于 2017-07-12T15:05:14.727 回答