当前位置: 首页 > 面试题库 >

字符串类型中的方法format(String,Object [])不适用于参数(String,int,int)

阳凌
2023-03-14
问题内容

好的,所以我是Java的新手,我正在尝试制作我的第一个程序,但收到此消息后,似乎无法修复:

The method format(String, Object[]) in the type String is not applicable for the arguments (String, int, int).

这是代码:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Gui extends JFrame{
    private JPanel GamePanel;
    private JLabel statusbar;

    public Gui(){
        super("Title");
        GamePanel = new JPanel();
        GamePanel.setBackground(Color.WHITE);
        add(GamePanel, BorderLayout.CENTER);

        statusbar = new JLabel("default");
        add(statusbar, BorderLayout.SOUTH);

        HandlerClass handler = new HandlerClass();
        GamePanel.addMouseListener(handler);
    }

    class HandlerClass implements MouseListener{
        public void mouseEntered(MouseEvent event){
            //here is where the problem is!
            statusbar.setText(String.format("%d, %d", event.getX(), event.getY());
        }

        public void mouseClicked(MouseEvent event) {
            statusbar.setText("ok?");
        }

        public void mouseExited(MouseEvent event) {
            statusbar.setText("something");
        }

        public void mousePressed(MouseEvent event) {
            statusbar.setText("something");
        }

        public void mouseReleased(MouseEvent event) {
            statusbar.setText("something");
        }

    }
}

问题答案:

此类错误消息表示您为方法提供的参数类型与预期的参数类型之间存在差异。在这种情况下,它要求一个(Object
[])String和一个数组,Object但您要给它一个String和两个Integer。您首先需要将它们打包为数组。

int [] temp = {event.getX(), event.getY()};
statusbar.setText(String.format("%d, %d", temp);


 类似资料: