如何自动调整JFrame上的图表大小?

时间:2016-03-29 14:39:40

标签: java swing resize jpanel jfreechart

我编写了一个代码,可以在JFrame上创建两个散点图和两个JPanel。 但是,在调整窗口大小时,我无法使它们自动适应窗口的大小。 以下是其中一个绘图类的代码:

class ALTCanvas extends JPanel{
    private static final long serialVersionUID = 1L;
    private XYSeries alts = new XYSeries("Altitude");

    public ALTCanvas() {
        final ChartPanel chartPanel = createDemoPanel();
        this.add(chartPanel, BorderLayout.CENTER);
    }

    private void update(double xnew, double ynew) {
        alts.add(new XYDataItem(xnew, ynew));
    }

    private ChartPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            "", "Time [epoch]", "Altitude [m]", createSampleData(),
            PlotOrientation.VERTICAL, false, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        XYItemRenderer renderer = xyPlot.getRenderer();
        NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
        range.setTickUnit(new NumberTickUnit(0.5));
        return new ChartPanel(jfreechart){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
    }

    private XYDataset createSampleData() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        xySeriesCollection.addSeries(alts);
        return xySeriesCollection;
    }

    private String stdCalc(){
        double sum = 0, avg = 0;
        double var = 0;
        for(int i = 0; i < alts.getItemCount(); i++){
            sum += (double) alts.getY(i);
        }
        avg = sum/alts.getItemCount();
        for(int i = 0; i < alts.getItemCount(); i++){
            var += ((double)alts.getY(i) - avg)*((double)alts.getY(i) - avg);
        }
        String str = "";
        str += Math.sqrt(var);
        return str;
    }
}

以下是图形初始化的代码:

public void initGrafics() {
    // create and set up the window
    JFrame frame = new JFrame("Position / Altitude");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // set up the content pane
    Container C = frame.getContentPane();

    // create canvas objects
    PC = new POSCanvas();
    PC.setBorder(BorderFactory.createRaisedBevelBorder());
    AC = new ALTCanvas();
    AC.setBorder(BorderFactory.createRaisedBevelBorder());

    JPanel left = new JPanel();
    left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    JLabel prcP = new JLabel("Precision [m]: "); JLabel acrP = new JLabel("Accuracy [m]: ");
    prcTp = new JTextField("          "); acrTp = new JTextField("          ");
    JPanel labelsP = new JPanel();
    labelsP.setLayout(new FlowLayout());
    labelsP.add(prcP); labelsP.add(prcTp); labelsP.add(acrP); labelsP.add(acrTp);
    left.add(PC); left.add(labelsP);

    JPanel right = new JPanel();
    right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    JLabel stdA = new JLabel("Standard deviation [m]: "); stdTa = new JTextField("          ");
    JPanel labelsA = new JPanel();
    labelsA.setLayout(new FlowLayout());
    labelsA.add(stdA); labelsA.add(stdTa);
    right.add(AC); right.add(labelsA);

    // add objects to the pane 
    C.setLayout(new BoxLayout(C, BoxLayout.X_AXIS));
    C.add(left); C.add(right);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    frame.setLocation(500, 500);
}

以下是调整大小后的样子:

enter image description here

可以看出,图表不符合窗口的大小。 任何人都可以帮我解决这个问题吗? 任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

JPanel的默认布局为FlowLayout,“允许每个组件采用其自然(首选)大小”,同时排除后续更改。在此上下文中,BorderLayout.CENTER参数无意义。而是将两个面板添加到GridLayout(1, 0)。这会将图表并排放置,并允许它们随着封闭容器的大小调整而增大和缩小。引用了一个完整的例子here

image