gpt4 book ai didi

java - swtchart 不显示在对话框中

转载 作者:行者123 更新时间:2023-12-02 05:35:43 25 4
gpt4 key购买 nike

我使用 swtchart ( http://www.swtchart.org/ ) 并尝试在对话框中显示它但总是失败。

SWT 对话框不支持 swtchart,是吗?

public final class TestDialog extends
Dialog {

private Chart chart;

private static double[] ySeries1 = {1,2,3,4,5,6,7,8,9,0};

public TestDialog(Shell shell) {
super(shell);
}

protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
createChart(composite);

return composite;
}


static public Chart createChart(Composite parent) {

// create a chart
Chart chart = new Chart(parent, SWT.NONE);

// set titles
chart.getTitle().setText("Large Series");
chart.getAxisSet().getXAxis(0).getTitle().setText("Data Points");
chart.getAxisSet().getYAxis(0).getTitle().setText("Amplitude");

// create line series
ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE,
"line series");
lineSeries.setYSeries(ySeries1);
lineSeries.setSymbolSize(2);
// adjust the axis range
chart.getAxisSet().adjustRange();

return chart;
}
}

最佳答案

我认为这个问题与 SWTChart 无关,而是与您对 SWT 布局的了解有关。

此外,图表没有理由不能在 SWT 对话框中工作。

请相应地修改您覆盖的createDialogArea:

@Override
protected void createDialogArea(final Composite parent)
{
final Composite dialogArea = (Composite) super.createDialogArea(parent);

final Composite container = new Composite(dialogArea, SWT.NULL);
container.setLayout(new GridLayout());
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

createChart(container);

return dialogArea;
}

不要直接在 dialogArea 下创建内容,而是创建一个中间容器。我碰巧知道 dialogArea 有一个 GridLayout 布局,因此我在容器上设置了 GridData

您可能想要read this article very carefully .

<小时/>

编辑1

很明显,您的图表正在绘制,但对话框已打包,您会觉得它没有绘制。尝试下面的代码,并调整对话框大小以查看图表。

使对话框适合图表是一个完全不同的问题(关于 SWT shell 和布局)。

/**
*
* @author ggrec
*
*/
public class ChartDialog extends Dialog
{

// ====================== 2. Instance Fields =============================

private static double[] ySeries1 = {1,2,3,4,5,6,7,8,9,0};


// ==================== 3. Static Methods ====================

public static void main(final String[] args)
{
final Display display = new Display();

new ChartDialog(new Shell(display)).open();

display.sleep();
display.dispose();
}


// ==================== 4. Constructors ====================

public ChartDialog(final Shell parent)
{
super(parent);
}


// ==================== 5. Creators ====================

@Override
protected Composite createDialogArea(final Composite parent)
{
final Composite dialogArea = (Composite) super.createDialogArea(parent);

final Composite container = new Composite(dialogArea, SWT.NULL);
container.setLayout(new FillLayout());
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

createChart(container);

return dialogArea;
}


@Override
protected void setShellStyle(final int newShellStyle)
{
super.setShellStyle(newShellStyle | SWT.RESIZE);
}


static private Chart createChart(final Composite parent)
{
// create a chart
final Chart chart = new Chart(parent, SWT.NONE);

// set titles
chart.getTitle().setText("Large Series");
chart.getAxisSet().getXAxis(0).getTitle().setText("Data Points");
chart.getAxisSet().getYAxis(0).getTitle().setText("Amplitude");

// create line series
final ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, "line series");
lineSeries.setYSeries(ySeries1);
lineSeries.setSymbolSize(2);
// adjust the axis range
chart.getAxisSet().adjustRange();

return chart;
}

}

关于java - swtchart 不显示在对话框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24992688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com