java无法调用第二类
问题描述:
我想从我的主类中的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);
}
}
答
这一行:mp = new getDataForDisplay(i);
有很多语法错误的:new someMethodCall(...)
是不允许的,getDataForDisplay(...)
有返回类型无效等。正确的是
mp = new MainPanel();
mp.getDataForDisplay();
答
mp = new getDataForDisplay(i);
是没有意义的,
- 删除
new
关键字如果要调用该方法 -
mainPanel mp = new mainPanel();
如果要创建mainPanel
类型的对象 - 根据惯例,类名应以大写字母开始,所以
mainPanel
应该MainPanel
如果你没有得到之间的类,对象,方法,实例化......好吧,我会开始的差别从才去任何进一步
你有没有导入类'mainPanel'? 如果不是,您可以使用完全合格的className进行初始化 –
@SashiKant是否指向mainPanel mp? – lecardo
您的主类是否在导入声明中包含'mainPanel'类? –