Java实际和形式参数列表的长度不同?

时间:2013-03-26 22:27:54

标签: java

我正在为我的编程类开发游戏,我在Eclipse中编写了没有可见错误的代码,但是当我编译它时,我收到以下错误消息:

  

TogizKumalak20.java:220:错误:类board20中的方法moveBoard不能应用于给定的类型;   playBoard.moveBoard(playTurn,keyCharAsInt);   要求:找到cup20:int,int   原因:实际和正式的参数列表长度不同

我不确定导致此问题的原因以及解决方法。非常感谢任何意见,并提前感谢您!

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Cursor;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.io.*;
import javax.swing.JFileChooser;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class TogizKumalak20
{
    public static void main(String[] args)
    {
        TKFrame myFrame = new TKFrame();
        myFrame.setTitle("Togiz Kumalak U22");
        myFrame.setVisible(true);
    }
}

class TKFrame extends JFrame
{
    private TKPanel myPanel;
    private final int FRAMEW = 700;
    private final int FRAMEH = 700;

    public TKFrame()
    {
        setSize(FRAMEW, FRAMEH);

        myPanel = new TKPanel();
        Container contentPane = getContentPane();

        contentPane.add(myPanel);
        addWindowListener(new WindowCloser());

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);

        JMenuItem restartItem = new JMenuItem("Restart");
        fileMenu.add(restartItem);

        RestartListener myRestartListener = new RestartListener();
        restartItem.addActionListener(myRestartListener);

        JMenuItem exitItem = new JMenuItem("Exit");
        fileMenu.add(exitItem);

        ExitListener myExitListener = new ExitListener();
        exitItem.addActionListener(myExitListener);
    }

    private class WindowCloser extends WindowAdapter
    {
        public void windowClosing(WindowEvent event)
        {
            dispose();
        }
    }

    private class RestartListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {

            myPanel.renewBoard();

            myPanel.renewTextArea();

            myPanel.setMoveCount(1);
        }
    }

    private class ExitListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            System.exit(0);
        }
    }
}

class TKPanel extends JPanel
{
    private board20 playBoard;
    private int playTurn;
    private JTextArea actionRecordArea;
    private JScrollPane actionScrollPane;
    private int moveCount = 1;

    public TKPanel()
    {
        super();
        playTurn = 1;
        playBoard = new board20();
        actionRecordArea = new JTextArea(6, 24);
        actionRecordArea.setEditable(false);
        actionScrollPane = new JScrollPane(actionRecordArea,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(actionScrollPane, "North");
        addMouseListener(new MouseHandler());
        addMouseMotionListener(new MouseMotionHandler());
        addKeyListener(new KeyHandler());
        setFocusable(true);
    }

    public void renewBoard()
    {
        playBoard = new board20();
        repaint();
    }

    public void renewTextArea()
    {
        actionRecordArea.setText("");
        repaint();
    }

    public int getMoveCount()
    {
        return moveCount;
    }

    public void setMoveCount(int countIn)
    {
        moveCount = countIn;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        playBoard.drawBoard(g2);
    }

    private class MouseHandler extends MouseAdapter
    {
        public void mouseClicked(MouseEvent event)
        {
            cup20 returnedCup;

            returnedCup = playBoard.isTheMouseInaCup(event.getPoint());

            if(returnedCup != null)
            {
                if(returnedCup.getWhoseCup() == playTurn)
                {
                    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

                    playBoard.moveBoard(returnedCup);

                    int whatCupNumber = playBoard.getCupNumber(returnedCup);
                    actionRecordArea.append("Move:  " + moveCount + ".  Player:  " + playTurn + ".  Cup:  " + whatCupNumber + ".\n");
                    moveCount++;

                    if(playTurn == 1)
                      playTurn = 2;
                    else
                      playTurn = 1;

                    repaint();
                }
            }
        }
    }

    private class MouseMotionHandler extends MouseMotionAdapter
    {
        public void mouseMoved(MouseEvent event)
        {
            cup20 retCup = playBoard.isTheMouseInaCup(event.getPoint());

            if(retCup == null)
              setCursor(Cursor.getDefaultCursor());
            else
            {
                if(retCup.getWhoseCup() == playTurn)
                  setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                else
                  setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            }
        }
    }

    private class KeyHandler extends KeyAdapter
    {
        public void keyTyped(KeyEvent event)
        {
            char keyChar = event.getKeyChar();

            int keyCharAsInt;

            if(keyChar == '1' || keyChar == '2' || keyChar == '3' || keyChar == '4' || keyChar == '5' || keyChar == '6' || keyChar =='7' || keyChar == '8' || keyChar == '9')
            {
                keyCharAsInt = (int) keyChar - 48;

                setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

                playBoard.moveBoard(playTurn, keyCharAsInt);

                actionRecordArea.append("Move:  " + moveCount + ".  Player:  " + playTurn + ".  Cup:  " + keyCharAsInt + ".\n");
                moveCount++;

                if(playTurn == 1)
                  playTurn = 2;
                else
                  playTurn = 1;

                repaint();
            }
            else
            {
            }
        }

        public board20 getBoard()
        {
            return playBoard;
        }

        public String getText()
        {
            return actionRecordArea.getText();
        }

        public void setText(String textIn)

    {
            actionRecordArea.setText(textIn);
        }
    }

}

2 个答案:

答案 0 :(得分:2)

(粗体表示原始错误消息。)

TogizKumalak20.java 220 方法中,使用给定类型来调用类板20中的方法moveBoard (它被称为 playBoard.moveBoard(playTurn,keyCharAsInt),因为该方法被声明为采用单个 cup20 参数但有两个参数( int,int )用于方法调用,因此实际和形式参数列表的长度不同

答案 1 :(得分:0)

你有另一个只接受一个参数的moveBoard调用。你有理由认为有一个版本的方法需要两个整数作为args吗?如果是这样,你使用的是正确版本的board20吗?这是你的课程还是图书馆课程?你没有为它提供资源。

错误告诉您没有方法签名的方法(参数的数量和类型构成签名)。在编译器可用的类版本中没有这样的方法。