gpt4 book ai didi

java - JPanel 内可调整大小的 JFreeChart

转载 作者:行者123 更新时间:2023-12-01 13:15:37 25 4
gpt4 key购买 nike

我知道这个问题之前已经被问过( hereherehere ),但提供的解决方案不起作用。因此,请不要将其标记为重复。我正在使用 JFreeChart 绘制折线图。然后我将此图表放入 JPanel 中,然后将其放入 tabbedPane 中。我知道直接将 JPanel 放入 JFrame 中可以使其调整大小,但我怎样才能这样做呢?

public class DynamicLineAndTimeSeriesChart extends JPanel implements ActionListener {
private ArrayList<TimeSeries> Series = new ArrayList<TimeSeries>();
private ArrayList<Double> Last = new ArrayList<Double>();
private String Id1;
private Timer timer = new Timer(250, this);
public DynamicLineAndTimeSeriesChart(String id) {

Runtime runtime = Runtime.getRuntime();
int NoOfProc = runtime.availableProcessors();
for (int i = 0; i < NoOfProc; i++) {
Last.add(new Double(100.0));
Series.add(new TimeSeries("Core" + i, Millisecond.class));
}
Id1 = id;
final TimeSeriesCollection dataset = new TimeSeriesCollection();
for (int i = 0; i < NoOfProc; i++) {
dataset.addSeries(this.Series.get(i));
}
final JFreeChart chart = createChart(dataset);
timer.setInitialDelay(1000);
chart.setBackgroundPaint(Color.LIGHT_GRAY);

//Created JPanel to show graph on screen
final JPanel content = new JPanel(new GridLayout());
final ChartPanel chartPanel = new ChartPanel(chart);
content.add(chartPanel, BorderLayout.CENTER);
content.revalidate();
this.add(content);

timer.start();
}

createChart 是 JFreeChart 类型的方法。这个类在另一个类中被调用

JPanel main = new JPanel(new BorderLayout());
main.add(demo);
jTabbedPane1.add("Core", main);
demo.setVisible(true);

框架设计是使用 NetBeans 完成的。我已经尝试了所有解决方案,例如将布局从 BorderLayout 更改为 GridBagLayout,甚至是提到的 here .

最佳答案

BorderLayout.CENTER 应该让 ChartPanel 随着框架大小的调整而增长。我猜您有一个带有 FlowLayoutJPanel,它继续使用图表面板的首选大小。由于 FlowLayout 是默认设置,因此您可能需要查找它。

编辑:有一个完整的示例 here将折线图添加到选项卡式 Pane 中。

编辑:您的DynamicLineAndTimeSeriesChart具有默认的FlowLayout;我想你想要一个GridLayout

public DynamicLineAndTimeSeriesChart(String id) {
this.setLayout(new GridLayout());

}

关于java - JPanel 内可调整大小的 JFreeChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22499283/

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