我正在尝试从主类访问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);

    }
}

最佳答案

此行:mp = new getDataForDisplay(i);有很多语法错误:不允许new someMethodCall(...)getDataForDisplay(...)具有returntype void,等等。正确的是

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

08-16 04:52