String类型中的方法格式(String,Object [])不适用

时间:2015-05-20 12:40:23

标签: java string format

到目前为止这是我的班级,但并不完整。我收到以下错误信息,我无法弄清楚为什么会这样!

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

该错误强调了我的类文件底部的String.format(...)方法。有关为何发生此错误的任何想法?

package buckysTutorial52;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{

    private JTextField item1;
    private JTextField item2;
    private JTextField item3;
    private JPasswordField passwordField;

    public tuna(){
        super("The title");
        setLayout(new FlowLayout());

        item1 = new JTextField(10);
        add(item1);

        item2 = new JTextField("enter text here");
        add(item2);

        item3 = new JTextField("uneditable", 20);
        item3.setEditable(false);
        add(item3);

        passwordField = new JPasswordField("mypass");
        add(passwordField);

        thehandler handler = new thehandler();
        item1.addActionListener(handler);
        item2.addActionListener(handler);
        item3.addActionListener(handler);
        passwordField.addActionListener(handler);

    }

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

            String string = "";

            if (event.getSource() == item1)     // If errors persist, return to tutorial #53
            {
                string = String.format("Field 1: %s", event.getActionCommand());
            }
            else if (event.getSource() == item2)
            {
                string = String.format("Field 2: %s", event.getActionCommand());
            }
            else if (event.getSource() == item3)
            {
                string = String.format("Field 3: %s", event.getActionCommand());
            }
            else if (event.getSource() == passwordField)
            {
                string = String.format("Password Field: %s", event.getActionCommand());
            }
        }

    }

}

0 个答案:

没有答案