JFreeChart用add加到panel上后没有显示出图来的解决

	        BotPanel = new JPanel();
	  	BotPanel.setBounds(10, 300, 330, 250);
	  	BotPanel.setLayout(null);
	  	BotPanel.setBorder(BorderFactory.createTitledBorder("使用情况"));
	  	BotPanel.add(new ChartTest().getChartPanel());  
		this.add(BotPanel);

一开始从网上找了个JFreeChart的样例,想先直接把图显示在自己的JPanel里看看效果,怎么都显示不出来,代码如上,后来发现是因为这种写法没有设置大小,导致没法很好地显示,于是先定义了个变量接收这个ChartPanel,然后用setBounds函数设置一下就可以了,修改后代码如下:

                BotPanel = new JPanel();
	  	BotPanel.setBounds(10, 300, 330, 250);
	  	BotPanel.setLayout(null);
	  	BotPanel.setBorder(BorderFactory.createTitledBorder("使用情况"));
	  	ChartPanel cp = new ChartTest().getChartPanel();
	  	cp.setBounds(50, 20, 200, 200);	 
	  	BotPanel.add(cp); 	  
		this.add(BotPanel);

JFreeChart相关jar包下载传送门:(这只是其中一个,其他的在这个网页上也能找到,比较好找,就不一一附上了)

点击打开链接

猜你喜欢

转载自blog.csdn.net/sinyusin/article/details/80315157