我试图从我的主类中读取java中的类的方法。编译器似乎不明白我在做什么。
它显示编译器错误:error cannot find symbol
mp = new getDataForDisplay(i);
我要做的是访问此方法,该方法将值分配给该类的多个全局变量以绘制矩形。
此代码来自我的主要类(在某些区域中简化)
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 :(得分:1)
这一行:mp = new getDataForDisplay(i);
有很多语法错误:new someMethodCall(...)
不被允许,getDataForDisplay(...)
返回类型无效等等。正确的将是
mp = new MainPanel();
mp.getDataForDisplay();
答案 1 :(得分:0)
mp = new getDataForDisplay(i);
毫无意义,
new
关键字mainPanel mp = new mainPanel();
类型的对象,请mainPanel
mainPanel
应为MainPanel
如果你没有在阶级,对象,方法,实例化之间找到区别......那么,我会在继续之前从中开始