我添加了一个JFreeChart
to a JPanel
(用一个BorderLayout
),并且它是huge。我可以做些什么来让它变小吗?
public void generateChart()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//set the values of the chart
for(int i=0; i<8; i++)
{
dataset.setValue(income_array[i], "Income",
Double.toString(percent_array[i]));
}
JFreeChart chart = ChartFactory.createBarChart(
"Required Annual Income for a Variety of Interest Rates",
"Percent", "Income", dataset, PlotOrientation.VERTICAL,
false,true, false);
ChartPanel cp = new ChartPanel(chart);
chart.setBackgroundPaint(Color.white);
chart.getTitle().setPaint(Color.black);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.blue);
//cp.setMaximumDrawHeight(5);
//cp.setMaximumDrawWidth(5);
//cp.setZoomOutFactor(.1);
JPanel graph = new JPanel();
graph.add(cp);
middle.add(graph, BorderLayout.CENTER);
}
当您创建您的ChartPanel http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartPanel.html,您有几个影响结果的选项:
接受DEFAULT_WIDTH
and DEFAULT_HEIGHT
:680×420。
指定首选width
and height
在构造函数中。
Invoke setPreferredSize()
明确地如果合适的 https://stackoverflow.com/q/7229226/230513.
-
覆盖getPreferredSize()
动态计算大小。
@Override
public Dimension getPreferredSize() {
// given some values of w & h
return new Dimension(w, h);
}
选择layout http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html的容器的ChartPanel
将被添加。请注意,默认布局JPanel
is FlowLayout
,而JFrame
is BorderLayout
。作为一个具体的例子,ThermometerDemo https://stackoverflow.com/a/7602126/230513在构造函数中同时使用首选值和GridLayout
让容器允许动态调整大小。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)