java - 如何让内容显示在 ScrolledComposite 中

标签 java eclipse swt composite scrolledcomposite

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

  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 相当陌生,在查看了示例并阅读了滚动 Composite 的文档之后,这就是我对它的解释。任何帮助将不胜感激。

最佳答案

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

sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

关于java - 如何让内容显示在 ScrolledComposite 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45059872/

相关文章:

java - 列表排序实现的单元测试?

java - RCP 使用 Clear Workspace 启动产品

java - 如何更改 ScrolledComposite 的水平滚动增量?

java - 对 String[][] 意外输出进行排序

java - 在命令提示符中看不到 java 版本 14

java - NoSuchElementException : Unable to find element with id/Works fine in FireFox and Chrome but not in IE

eclipse - Eclipse Neon 搜索结果选项卡中的 'Expand All' 功能在哪里?

javascript - Eclipse "aptana plugin"与 jsdt(JavaScript 开发工具);我在哪里可以找到比较图表?

java - JFace CTabFolder,在选项卡之间切换

java - 连续查询数据库以进行更新 - java