本文介绍了如何自动调整JFrame上图表的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码,该代码创建了两个散布图和两个放置在JFrame上的JPanel.但是,在调整窗口大小时,我无法使它们自动适应窗口的大小.这是其中一个绘图类的代码:

I have wrote a code that creates two scatters and two JPanels placed on a JFrame.However I could not make them fit automatically to the size of the window when it is being resized.Here is the code for one of the drawing classes:

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;
    }
}

这是图形初始化的代码:

Here is the code for the initialization of the graphics:

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);
}

这是调整大小后的样子:

Here is what it looks like after resizing:

正如人们所看到的,图表不适合窗口的大小.有人可以帮我解决问题吗?任何帮助将不胜感激,谢谢.

As one can see, the charts does not fit the size of the window.Can anybody help me how to fix it?Any help would be appreciated, thank you.

推荐答案

JPanel FlowLayout ,它让每个组件都采用其自然的(首选)大小",同时排除了后续的更改.在这种情况下,BorderLayout.CENTER参数是没有意义的.而是将两个面板添加到GridLayout(1, 0).这将使图表并排放置,并允许它们随着调整封闭容器的大小而增大和缩小. 此处.

The default layout of JPanel is FlowLayout, which "lets each component assume its natural (preferred) size," while precluding subsequent changes. The BorderLayout.CENTER parameter is meaningless in this context. Instead, add the two panels to a GridLayout(1, 0). This will place the charts side-by-side and allow them to grow and shrink as the enclosing container is resized. A complete example is cited here.

这篇关于如何自动调整JFrame上图表的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:36