0
我想對齊JFrame中的整個圖表,但我知道的正常組件代碼不適用於JChartPanels。最初我試圖在JChartPanel上設置AligmentX和Y,但那不起作用。然後,我嘗試將JChartPanel添加到JPanel,然後setAlignment,但是一旦我將JChartPanel添加到JPanel,該圖不再可見。對齊jFreeChart(setAlignmentX和Y?)
下面的代碼在默認左上角位置的JFrame中創建一個圖。我需要對齊圖表 - 使用任何值,因爲稍後將更改它們以符合我的目的。
沒有任何上述的嘗試(錯誤)的所包含的代碼:
import java.awt.*;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ChartTest extends javax.swing.JFrame {
public ChartTest() {
XYSeries Goals = new XYSeries("Goals Scored");
Goals.add(1, 1.0);
Goals.add(2, 3.0);
Goals.add(3, 2.0);
Goals.add(4, 0.0);
Goals.add(5, 3.0);
XYDataset xyDataset = new XYSeriesCollection(Goals);
JFreeChart chart = ChartFactory.createXYLineChart("Goals Scored Over Time", "Fixture Number", "Goals", xyDataset, PlotOrientation.VERTICAL, true, true, false);
JPanel jPanel = new JPanel();
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
jPanel.setVisible(true);
jPanel.setSize(300, 300);
ChartPanel CP = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
CP.setMouseWheelEnabled(true);
add(CP);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
initComponents();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChartTest().setVisible(true);
}
});
}
您可能需要改變封閉面板的佈局,爲[示例](http://stackoverflow.com /一個/230513分之5630271)。 – trashgod
我不明白。我使用'new FlowLayout(FlowLayout.RIGHT,3,3)'改變了「父」jPanel的佈局,然後使用'jPanel.add(CP);'添加了ChartPanel,結果只是一個空窗口... – AppreciateTheHelp