我使用netbeans gui生成器生成了一个gui。我有两个面板,其中一个我想显示一个chartpanel。 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内部..我该怎么办?你能救我吗?
答案 0 :(得分:0)
将NetBeans面板中的Layout设置为BorderLayout(从设计预览中右键单击然后设置布局,或者从代码Panel.setLayout中执行),之后当您想要显示图表面板时,只需添加图表面板进入父母小组并要求重新粉刷。
netbeansPanel.add(charPanel, BorderLayout.CENTER);
netbeansPanel.validate();
如果您想使用新图表更新图表,也可以删除面板:
netbeansPanel.removeAll();
netbeansPanel.add(charPanel);
netbeansPanel.validate();