我想從我的主類中的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);
}
}
你有沒有導入類'mainPanel'? 如果不是,您可以使用完全合格的className進行初始化 –
@SashiKant是否指向mainPanel mp? – lecardo
您的主類是否在導入聲明中包含'mainPanel'類? –