在以下代码中,我收到错误“无法对非静态字段 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);
}
}