我试图在JDialog中显示一个JFreeChart并且它正确显示但是当我试图在模态JDialog中显示它时它不会显示。以下程序不显示JFreeChart图,直到我评论第31行。什么是原因是什么?
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphExample extends JDialog{
private final JPanel contentPanel = new JPanel(new BorderLayout());
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
GraphExample graph=new GraphExample(new JFrame(),"Testing");
graph.createGraphPanel();
} catch (Exception e) {
e.printStackTrace();
}
}
public GraphExample(JFrame parent,String title) {
super(parent,title);
setPreferredSize(new Dimension(500, 500));
this.setModal(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(contentPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void createGraphPanel(){
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1= new XYSeries("First");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
series1.add(3.0, 3.0);
series1.add(4.0, 5.0);
series1.add(5.0, 5.0);
series1.add(6.0, 7.0);
series1.add(7.0, 7.0);
series1.add(8.0, 8.0);
dataset.addSeries(series1);
final JFreeChart chart = createChart(dataset);//,xLabel,yLabel,title,seriesesVector,check);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(400,400));//width height
contentPanel.add(chartPanel,BorderLayout.CENTER);
contentPanel.revalidate();
}
private JFreeChart createChart(final XYDataset dataset){//, String xLabel, String yLabel, String title,Vector seriesesVector,String check) {
final JFreeChart result = ChartFactory.createXYLineChart(//createXYAreaChart(//also comment line#218 219 220 also uncomment line#250
null,
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
result.setTitle("Example");
final XYPlot plot = result.getXYPlot();//domain is x and range is y
plot.setDomainGridlinesVisible(false);
plot.setRangeGridlinesVisible(false);
return result;
}
}
如何在Modal JDialog中显示JFreeChart?
答案 0 :(得分:1)
请勿在JDialog上调用setVisible(true)
,直到所有内容设置为止。事实上,让它 out 的构造函数,并在声明和构造JDialog的代码中自己调用它,并且只在完成所有操作之后。了解代码流在调用时将被阻止,因此必须最后调用。
所以改变这个:
public GraphExample(JFrame parent,String title) {
super(parent,title);
setPreferredSize(new Dimension(500, 500));
this.setModal(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(contentPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
到此:
public GraphExample(JFrame parent,String title) {
super(parent,title);
setPreferredSize(new Dimension(500, 500));
this.setModal(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(contentPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
// setVisible(true); // **** **** **** ****
}
和此:
public static void main(String[] args) {
try {
GraphExample graph=new GraphExample(new JFrame(),"Testing");
graph.createGraphPanel();
graph.setVisible(true); // ****** add *****
} catch (Exception e) {
e.printStackTrace();
}
}