如何克服静态参考非静态字段错误?

时间:2013-10-27 13:30:36

标签: java

在下面的代码中,我收到错误'无法对非静态字段MainFrame.lambdaD进行静态引用'。我可以克服这个问题而不将lambdaD改为静态,如果是这样的话怎么办?

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;



/**
 *
 * @authors George and Stephen Tomlinson
 */
public class Graph extends Applet{

    // set version number

    private static final long serialVersionUID = 1L;

    /*
       Method which takes an ArrayList array defining 2 data sets and produces a JFreeChart
       graphing them and adds this to a JPanel which is then used by the createandShowGUI method to 
       define the contents of a JFrame GUI and then display it. The method returns this JPanel.
     */

    public JPanel createContentPane(ArrayList<Double>[] in){

        JPanel panel = new JPanel();

        panel.setLayout(new BorderLayout());

        XYSeries seriesTrue = new XYSeries("True");
        XYSeries seriesApprx = new XYSeries("Approx");

        // Read input arguments and store in XYSeries objects.

         for(int i=0;i<in[0].size();i++)
         {
             double item = (double)in[0].get(i);
             seriesTrue.add(i,item);
         }

         for(int i=0;i<in[1].size();i++)
         {
             double item = (double)in[1].get(i);
             seriesApprx.add(i,item);
         }        

        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeriesCollection datasetApprx = new XYSeriesCollection();
        dataset.addSeries(seriesTrue);
        datasetApprx.addSeries(seriesApprx);

        double lambda = MainFrame.lambdaD;

        // Create a chart from the first data set (the true solution). 

        JFreeChart chart = ChartFactory.createXYLineChart(
                "Plot of true and approx solns of y' = -" + lambda+ "*y",
                "time",
                "y",
                dataset, 
                PlotOrientation.VERTICAL,
                true,
                true,
                false
                );

        ChartPanel chartPanel = new ChartPanel(chart);

        // Add the second data set (the approximate solution) to the chart (the first is at index 0).

        chart.getXYPlot().setDataset(1, datasetApprx);

        /*
         Create a separate renderer for each data set (otherwise they won't be recognised as separate
         data sets, so the JFreeChart object won't give them different colours for example, as it only
         sees one data set, so refuses to assign any more than one colour to it.  
         * */

        XYSplineRenderer SR0 = new XYSplineRenderer(1000);

        XYSplineRenderer SR1 = new XYSplineRenderer(1000);

        chart.getXYPlot().setRenderer(0, SR0); 

        chart.getXYPlot().setRenderer(1, SR1); 

        // Set colours for each data set.

        chart.getXYPlot().getRendererForDataset(chart.getXYPlot().getDataset(0)).setSeriesPaint(0, Color.BLUE);

        chart.getXYPlot().getRendererForDataset(chart.getXYPlot().getDataset(1)).setSeriesPaint(0, Color.RED);

        panel.add(chartPanel);

        panel.setOpaque(true); 

        return panel;  
    }

    // Method to create the GUI upon which the graph is displayed, which takes an ArrayList array (called i)
    // of length 2 containing the 2 data sets to be graphed.

    public void createAndShowGUI(ArrayList<Double>[] i) {


        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame(" ODESolver ");

        // Create and set up the content pane.
        Graph demo = new Graph();
        frame.setContentPane(demo.createContentPane(i));

        // The other bits and pieces that define how the JFrame will work and what it will look like.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 650);
        frame.setVisible(true);

    }

}

3 个答案:

答案 0 :(得分:0)

实例化MainFrame

MainFrame mainFrame = new MainFrame();
double lambda = mainFrame.lambdaD;

答案 1 :(得分:0)

如果图形需要从创建它的MainFrame实例中获取值,则MainFrame实例应作为Graph构造函数或方法的参数传递。

(JB Nizet发表评论)

答案 2 :(得分:-1)

在您的方法createContentPane和下面的代码中

double lambda = MainFrame.lambdaD;

发出错误。 这是因为您没有创建类型为MainFrame的对象来调用其属性lambdaD。

尝试::

 double lambda = new MainFrame().lambdaD;