0

次のコードでは、「非静的フィールド MainFrame.lambdaD への静的参照を作成できません」というエラーが表示されます。lambdaD を static に変更せずにこれを克服できますか?

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

    }

}
4

3 に答える 3

0

メインフレームのインスタンス化

MainFrame mainFrame = new MainFrame();
double lambda = mainFrame.lambdaD;
于 2013-10-27T13:33:45.467 に答える
0

グラフがそれを作成する MainFrame インスタンスから値を取得する必要がある場合は、MainFrame インスタンスを Graph コンストラクタまたはメソッドの引数として渡す必要があります。

(JB Nizetによるコメントで回答)

于 2013-10-27T14:08:34.743 に答える
-1

あなたの方法createContentPaneと以下のコードで

double lambda = MainFrame.lambdaD;

エラーを出しています。MainFrameこれは、そのプロパティ lambdaD を呼び出すための型のオブジェクトを作成していないためです。

試す::

 double lambda = new MainFrame().lambdaD;
于 2013-10-27T13:32:05.590 に答える