gpt4 book ai didi

java - 如何在NetBeans生成的Jpanel中显示JfreeChart(ChartPanel)

转载 作者:行者123 更新时间:2023-12-01 05:25:46 30 4
gpt4 key购买 nike

我已经使用 netbeans gui 生成器生成了一个 GUI。我有两个面板,其中一个我想显示一个图表面板。 netbeans生成的代码是:

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 394, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 294, Short.MAX_VALUE)
);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
}

我将图表面板添加到 jpanel 所做的是:

TimeSeriesCollection dataset = new TimeSeriesCollection();
for (int i = 0; i < 5; i++) {
TimeSeries pop = new TimeSeries("try "+i, Day.class);
pop.add(new Day(10+i, 1+i, 2004+i), 100*(i+2));
pop.add(new Day(10+1*i, 1+2+i, 2004+i+1), 150*(i+2));
pop.add(new Day(10+2*i, 1+2*i, 2004+i+2), 150*(i+3));
pop.add(new Day(10+3*i, 1+2*i, 2004+i+3), 150*(i+4));
pop.add(new Day(10+4*i, 1+2*i, 2004+i+10), 150*(i+5));
dataset.addSeries(pop);
}
TimeSeries pop = new TimeSeries("Try asjhaihsiuahsiuahiushaiushiauhsiuhasiua"+100, Day.class);
int i=5;
pop.add(new Day(1, 1+i, 2004+i), 100*(i+2));
pop.add(new Day(2, 1+2+i, 2004+i+1), 150*(i+2));
pop.add(new Day(3, 1+2*i, 2004+i+2), 150*(i+3));
pop.add(new Day(4, 1+2*i, 2004+i+3), 150*(i+4));
pop.add(new Day(5, 1+2*i, 2004+i+10), 1500*(i+5));
dataset.addSeries(pop);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Population of CSC408 Town",
"Date",
"Population",
dataset,
true,
true,
false);
ChartPanel p = new ChartPanel(chart);

jPanel1.add(p);

但是图表面板不会显示在 jpanel 内..我该怎么办?你能帮我吗?

最佳答案

将 NetBeans 面板中的布局设置为 BorderLayout (通过在设计预览中右键单击它然后设置布局,或通过代码 Panel.setLayout 进行设置),之后当您想要显示图表面板时,只需添加图表面板进入父面板并要求重新绘制。

netbeansPanel.add(charPanel, BorderLayout.CENTER);
netbeansPanel.validate();

如果您想用新面板更新图表,您也可以删除该面板:

netbeansPanel.removeAll();
netbeansPanel.add(charPanel);
netbeansPanel.validate();

关于java - 如何在NetBeans生成的Jpanel中显示JfreeChart(ChartPanel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9689859/

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