字符串类型中的方法format(String,Object [])不适用于参数(String,int,int) [英] The method format(String, Object[]) in the type String is not applicable for the arguments (String, int, int)

查看:55
本文介绍了字符串类型中的方法format(String,Object [])不适用于参数(String,int,int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

okay so I'm new to Java and I'm trying to make my first program and i get this message i cant seem to fix:

String类型的方法format(String,Object [])不适用于参数(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");
        }

    }
}

推荐答案

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

Error messages of this type indicate a difference between the types of parameters you're giving the method, and the types it expects. In this case it is asking for a String and an Array of Object(Object[]) but you're giving it a String and two Integers. You need to package these as an Array first.

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

这篇关于字符串类型中的方法format(String,Object [])不适用于参数(String,int,int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆