我在(使用)中添加了JFreeChart一个,它很大。有什么办法可以做得更小?JPanelBorderLayout
JFreeChart
JPanelBorderLayout
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,你有几个影响结果的选项:
ChartPanel
DEFAULT_WIDTH
DEFAULT_HEIGHT:680 x 420
width
height
setPreferredSize()
getPreferredSize()
@Override public Dimension getPreferredSize() { // given some values of w & h return new Dimension(w, h); }
选择将添加到的容器的布局ChartPanel。需要注意的是默认的布局JPanel是FlowLayout,而的JFrame是BorderLayout。作为一个具体示例,ThermometerDemo在构造函数中使用首选值,并GridLayout在容器中同时使用,以允许动态调整大小。
JPanel
FlowLayout
JFrame
BorderLayout
ThermometerDemo
GridLayout