我試過尋找答案來得到這個工作,但沒有設法做到這一點,我即將失去理智,所以這裏就是我的問題。如何將JFreechart添加到面板?
我最近下載了JFreeChart來創建這樣的圖表,並在GUI中實現它們。 這裏的GUI和我想要的插入圖表面板(標記爲白色):
reports1 http://www.freeimagehosting.net/newuploads/eff3r.png reports2 http://www.freeimagehosting.net/newuploads/v5sty.png
我已經使用NetBeans編輯構建GUI和JFreeChart的例子可在網上。 下面是我的GUI代碼:
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
public class Report extends JFrame implements Defaults {
public MMap j1map,j2map,j3map;
public int total_stations = 0, total_jobs = 0, total_parts = 0;
BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
public String sim_time;
BigDecimal systime = new BigDecimal(0);
BigDecimal worktime = new BigDecimal(0);
BigDecimal idletime = new BigDecimal(0);
BigDecimal queuetime = new BigDecimal(0);
/**
* Creates new form Report
*/
public Report() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(TITLE_Report);
validate();
setResizable(false);
setVisible(true);
//setLocationRelativeTo(null);
initComponents();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
}
);
centertables();
//JLayeredPane layeredPane = new JLayeredPane();
PieChart demo = new PieChart("Which operating system are you using?");
demo.setOpaque(true);
demo.setBounds(0, 0, 300, 300);
demo.setFocusable(true);
demo.setBackground(Color.gray);
demo.setBorder(BorderFactory.createLineBorder(Color.black, 1));
//start();
demo.setDoubleBuffered(true);
//demo.pack();
//demo.setVisible(true);
//PieChart("teste");
chartPanel1.add(demo, new Integer(0));
//layeredPane.add(demo, new Integer(0));
//this.getContentPane().add(jLayeredPane1);
}
/* *************************************
* GRAPHICS
*************************************/
public void PieChart(String chartTitle) {
System.out.println("PieChart");
// This will create the dataset
PieDataset dataset = createDataset();
// based on the dataset we create the chart
JFreeChart chart = createChart(dataset, chartTitle);
// we put the chart into a panel
ChartPanel chartPanel = new ChartPanel(chart);
// default size
chartPanel.setPreferredSize(new java.awt.Dimension(250, 270));
// add it to our application
//setContentPane(chartPanel);
//StationsPanelGraph.add(chartPanel);
//jLayeredPane1.add(chartPanel, new Integer(0), 0);
}
private PieDataset createDataset() {
System.out.println("PieDataset");
DefaultPieDataset result = new DefaultPieDataset();
result.setValue("Linux", 29);
result.setValue("Mac", 20);
result.setValue("Windows", 51);
return result;
}
private JFreeChart createChart(PieDataset dataset, String title) {
System.out.println("Create Chart");
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
dataset, // data
true, // include legend
true,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
/* *************************************
* END GRAPHICS
*************************************/
}
這是結果:
reports3 http://www.freeimagehosting.net/newuploads/unolz.png
我初始化和添加圖表中的主類的構造函數和右下方是代碼圖表。我必須避免發佈整個代碼,因爲它超出了字符限制,但是這裏是完整代碼的URL(通過NetBeans初始化Swing組件):http://tny.cz/8a4e8b2f
任何解決方案的提示?
提前謝謝,讓我知道是否需要其他東西。
的解決方案是有可能在加入的JFreeChart面板(我相信這是不是一個JPanel而已),當使用正確的佈局。如果您熟悉可供您使用的各種佈局管理器,將JFreeChart組件放置到您想放置它的位置應該是一件輕而易舉的事情。 –
@Hovercraft:確切地說; 'org.jfree.chart.ChartPanel'是一個'JPanel'。它具有一個'org.jfree.chart.JFreeChart',它在'paintComponent()'中呈現。另請參閱此[示例](http://stackoverflow.com/a/5522583/230513)。 – trashgod
當我開始使用NetBeans時,我已經習慣使用它的編輯器來設計GUI,這意味着我不完全理解作爲其中之一的所有概念佈局。我會進一步研究它! –