如何将复合添加到 SWT 中的滚动容器复合



我正在尝试创建一个可滚动的容器窗口,我可以根据来自不同文件的一些值将 JFreeChart 步骤图表一个接一个地添加到其中,但我遇到了几个问题。其中之一是我无法将 ChartComposite 对象放入容器中,一旦我运行程序,它就会显示一个空窗口。我肯定做错了什么,但老实说我不知道它应该如何。

以下是我如何尝试将一些图表放入容器的代码。

public void createPartControl(Composite parent) {
    final ScrolledComposite scrolled = new ScrolledComposite(parent, SWT.V_SCROLL);
    Composite comp = new Composite(scrolled, SWT.NONE);
    scrolled.setLayout(new FillLayout(SWT.VERTICAL));
    final JFreeChart chart = createChart();
    final JFreeChart chart1 = createChart();
    final JFreeChart chart2 = createChart();
    final JFreeChart chart3 = createChart();
    new ChartComposite(comp, SWT.NONE, chart, true);
    new ChartComposite(comp, SWT.NONE, chart1, true);
    new ChartComposite(comp, SWT.NONE, chart2, true);
    new ChartComposite(comp, SWT.NONE, chart3, true);
    comp.setLayout(new FillLayout(SWT.VERTICAL));
    scrolled.setContent(comp);
    scrolled.setExpandVertical(true);
    scrolled.setExpandHorizontal(true);
    scrolled.setAlwaysShowScrollBars(true);
    scrolled.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            org.eclipse.swt.graphics.Rectangle r = scrolled.getClientArea();
            scrolled.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
        }
    });
}

欢迎任何帮助或指向有关如何做这样的事情的一些出色教程的链接。

编辑:确实尝试了一点ScrolledComposite,相应地修改了代码,但它扩展了图表以适应整个视图,并且绝不是可滚动的。

下面是一个工作示例供您调整:

public static void main( String[] args ) {
  Display display = new Display();
  Shell shell = new Shell( display );
  shell.setLayout( new FillLayout() );
  ScrolledComposite scrolled = new ScrolledComposite( shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL );
  scrolled.setExpandVertical( true );
  scrolled.setExpandHorizontal( true );
  scrolled.setAlwaysShowScrollBars( true );
  Composite composite = new Composite( scrolled, SWT.NONE );
  composite.setLayout( new FillLayout( SWT.VERTICAL ) );
  for( int i = 0; i < 6; i++ ) {
    Composite item = new Composite( composite, SWT.NONE );
    item.setBackground( item.getDisplay().getSystemColor( SWT.COLOR_BLACK + i ) );
  }
  scrolled.setContent( composite );
  scrolled.setMinSize( composite.computeSize( SWT.DEFAULT, SWT.DEFAULT ) );
  scrolled.addControlListener( new ControlAdapter() {
    public void controlResized( ControlEvent event ) {
      Rectangle clientArea = scrolled.getClientArea();
      scrolled.setMinSize( composite.computeSize( clientArea.width, SWT.DEFAULT ) );
    }
  } );
  shell.setSize( 300, 300 );
  shell.open();
  while( !shell.isDisposed() ) {
    if( !display.readAndDispatch() )
      display.sleep();
  }
  display.dispose();
}

item s 代表您的CharComposite。如果这没有显示所需的结果,则需要修改ChartComposite::computeSize()实现,或使用单列GridLayout并通过GridData::widthHintheightHint控制图表的大小。

最新更新