当前位置: 动力学知识库 > 问答 > 编程问答 >

Java parameters errors, please assist

问题描述:

can't get this code to compile, I get an error that says:

invalid method declaration; return type required public MineFinderFrame(int nc, int nr,int mod)

please can anyone amend my code?

class MineFinderPanel extends JPanel implements MouseListener

{

int numCols;

int numRows;

int myModel;

public static void main(String[] args)

{

MineFinderFrame w = new MineFinderFrame(30,50,42);

w.numRows = 50; // 50 rows

w.numCols = 20; // 20 columns

w.myModel = 42;

w.setVisible(true);

}

public MineFinderFrame(int nc, int nr,int mod)

{

numCols = nc;

numRows = nr;

myModel = mod;

addMouseListener(this);

}

网友答案:

If you rename your class, you also have to rename the constructor in this class.

So I would start with this:

public MineFinderPanel(int nc, int nr,int mod)
{
   numCols = nc;
   numRows = nr;
   myModel = mod;
   addMouseListener(this);
}

And then, you would also have to change the first line in your main method to

MineFinderPanel w = new MineFinderPanel(30,50,42);
网友答案:

Your second function needs to declare a return type.

If your function is not supossed to return anything use void:

public void MineFinderFrame(int nc, int nr, int mod)
网友答案:

The method MineFinderFrame(int nc, int nr,int mod) does not say what type it returns.

Did you mean it to be the constructor? If so, is should be the same name as the class:

public MineFinderPanel(int nc, int nr,int mod) {
  numCols = nc;
  numRows = nr;
  myModel = mod;
  addMouseListener(this);
}
网友答案:

Is MineFinderFrame supposed to be a constructor?

If yes, it must have the same name as the class (MineFinderPanel, not MineFinderFrame). Otherwise, it is a normal method and it must have a return type.

网友答案:

You need a class named MindFinderFrame, if that's the type you are instantiating. Did you mean to name MindFinderPanel as MindFinderFrame? (Or, other way around, perhaps you should be calling,

MindFinderPanel m = new MindFinderPanel(...);

网友答案:
public MineFinderFrame (int nc, int nr,int mod)

is a constructor for a class called MineFinderFrame and you are trying to use it as a constructor of a class called MineFinderPanel.

分享给朋友:
您可能感兴趣的文章:
随机阅读: