如何在SWT中向滚动容器复合体中添加复合体[已关闭]。

问题描述 投票:0回答:1

我正在尝试创建一个可滚动的容器窗口,我可以根据不同文件中的一些值,将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));
        }
    });

}

任何帮助或一些伟大的教程的链接,如何做这样的东西将是欢迎的。

EDIT:确实用ScrolledComposite尝试了一下,相应地修改了代码,但它将图表扩展到适合整个视图,而且绝不是可滚动的。

java swt jfreechart
1个回答
2
投票

下面是一个工作实例,供你改编。

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代表您的 CharComposites. 如果没有显示出想要的结果,你需要修改 ChartComposite::computeSize() 实现,或者使用单列的 GridLayout 和控制图表的大小,通过 GridData::widthHintheightHint.

© www.soinside.com 2019 - 2024. All rights reserved.