【发布时间】:2014-03-15 00:05:50
【问题描述】:
我正在尝试在 JFrame 中对齐整个图表,但我知道的正常组件代码不适用于 JChartPanel。最初我尝试在 JChartPanel 上设置 AligmentX 和 Y 但这没有用。然后我尝试将 JChartPanel 添加到 JPanel 然后 setAlignment 但是一旦我将 JChartPanel 添加到 JPanel 图形不再可见。
下面的代码在默认左上角的 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);
}
});
}
【问题讨论】:
-
您可能需要更改封闭面板的布局,为example。
-
我不明白。我使用
new FlowLayout(FlowLayout.RIGHT, 3,3)更改了“父” jPanel 的布局,然后使用jPanel.add(CP);添加了 ChartPanel,结果只是一个空窗口...
标签: netbeans alignment jfreechart chartpanel