2015-04-20 31 views
0

我想從我的主類中的java類中訪問一個方法。編譯器似乎不明白我在做什麼。java無法調用第二類

它顯示了編譯器錯誤關閉:上線mp = new getDataForDisplay(i);

我所試圖做的是訪問這個方法中值分配給該類的幾個全局變量來繪製一個矩形error cannot find symbol

此代碼是從我的主類(在某些方面簡化)

main class 
-some other classes here 
-this is in my actionlistener...removed some irrelevant parts 
    //show graph 
      f1.add(new mainPanel(numProcc)); //shows data for graph 
      f1.pack(); 
      processDetail.dispose(); 
      //call up process builder 
      connect2c c = new connect2c(); // compile and run the C code 

      int i = 0; 
      String[] value = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; 
      mainPanel mp; 
      while (i < numProcc) 
      { 
       c.returnData(value[i]); 
       mp = new getDataForDisplay(i); 
       //openFile f = new openFile (value[i]); 
       i++; 
      } 

如果你在第五行注意到,我莫名其妙地管理連接到mainPanel中類。我發現這個代碼某個地方

這是我試圖訪問類的,我嘗試訪問的方法是getDataForDisplay()

class mainPanel extends JPanel 
{ 
    int xCoor =0; 
    int yCoor =0; 
    int width =0; 
    int height =0; 
    public mainPanel(int x) 
    { 
     //constructor stuff here 
    } 

    public void getDataForDisplay (int a) 
    { 
    //store in global variable 

    //width = num of processes x 20 
    //rect ->x,y,width,height 
    //int a -> how many quantums, not using yet 
    xCoor = 100; 
    yCoor = 150; 
    width = 50; 
    height = 50; 

    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 

    g.setColor(Color.RED); 
    g.fillRect (xCoor, yCoor, width, height); 

    } 
} 
+0

你有沒有導入類'mainPanel'? 如果不是,您可以使用完全合格的className進行初始化 –

+0

@SashiKant是否指向mainPanel mp? – lecardo

+0

您的主類是否在導入聲明中包含'mainPanel'類? –

回答

1

這一行:mp = new getDataForDisplay(i);有很多語法錯誤的:new someMethodCall(...)是不允許的,getDataForDisplay(...)有返回類型無效等。正確的是

mp = new MainPanel(); 
mp.getDataForDisplay(); 
0

mp = new getDataForDisplay(i);是沒有意義的,

  • 刪除new關鍵字如果要調用該方法
  • mainPanel mp = new mainPanel();如果要創建mainPanel類型的對象
  • 根據慣例,類名應以大寫字母開始,所以mainPanel應該MainPanel

如果你沒有得到之間的類,對象,方法,實例化......好吧,我會開始的差別從纔去任何進一步