2013-10-27 42 views
0

在以下代碼中,我收到錯誤「無法對非靜態字段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); 

    } 

} 
+2

您需要的主機 – tom

+0

實例,但主機創建圖表的一個實例,因此不會造成問題,如果是這樣,有沒有別的辦法? –

回答

-1

在你的方法createContentPane及以下

double lambda = MainFrame.lambdaD; 

代碼給錯誤。 這是因爲您沒有創建類型MainFrame的對象來調用其屬性lambdaD。

嘗試::

double lambda = new MainFrame().lambdaD; 
+0

但是MainFrame的一個實例已經被創建,它創建了一個Graph實例。這是來自MainFrame的lambdaD已經實例化,我需要得到。 –

0

實例化大型機

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

這將修復編譯錯誤,但它可能不會執行OP所要的操作。我的猜測是,某個地方已經有一個MainFrame的實例,並且他需要這個實例的mabdaD值。 –

+0

這就對了@JB Nizet –

+2

@ bluesh34:如果圖需要從創建它的MainFrame實例中獲取一個值,那麼MainFrame實例應該作爲Graph構造函數或方法的參數傳遞。爲了能夠與約翰談話,你需要約翰。所以,如果約翰想聽你的話,它應該來看你,並請你和他談談。當然,如果Graph中只需要labdaD的當前值,lambdaD也可以作爲參數傳遞給構造函數/方法,而不是整個MainFrame實例。 –

0

如果圖形需要得到從創建它的大型機實例的值,則大型機實例應的參數傳遞圖構造函數或方法。

(由JB Nizet在評論回答)

相關問題