import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class IllegalPasswordException extends Exception
{
public String getMessage()
{
return "Invalid password.please input password within 6~10 .";
}
};public class Customer extends JApplet
{
String custName;
String custPassword;
JPanel panelObject;
JLabel labelCustName;
JLabel labelCustPassword;
JTextField textCustName;
JPasswordField textCustPassword;
JButton buttonLogin;

void setPassword(String password) throws IllegalPasswordException
{
if ((password.length()<6) || (password.length()>10))
      throw new IllegalPasswordException();
custPassword=password; 
}
public void init()
{
panelObject = new JPanel();
getContentPane().add(panelObject);
panelObject.setLayout(new FlowLayout());
labelCustName= new JLabel("Customer Login Name:");
labelCustPassword = new JLabel("Password:");
textCustName = new JTextField(15);
textCustPassword = new JPasswordField(15);
buttonLogin = new JButton("Login");
panelObject.add(labelCustName);
panelObject.add(labelCustPassword);
panelObject.add(textCustName);
panelObject.add(textCustPassword);
panelObject.add(buttonLogin); LoginAction loginrequest = new LoginAction();
buttonLogin.addActionListener(loginrequest);
} class LoginAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==buttonLogin)
{

String password =textCustPassword.getPassword();
try
{
setPassword(password);
getAppletContext().showStatus("Valid entry for customer password!");
}
catch(IllegalPasswordException e)
{
getAppletContext().showStatus(e.getMessage());
}
}
}
}
};-------------------------------
---------- 编译 ----------
Customer.java:59: 不兼容的类型
找到: char[]
需要: java.lang.String
String password =textCustPassword.getPassword();
                                             ^
1 错误输出完成 (耗时 5 秒) - 正常终止

解决方案 »

  1.   

    看一下 textCustPassword.getPassword(); 中getPassword  的返回类型. 如果是char[] 类型 改成String 
    或 String password =new String(textCustPassword.getPassword());
    这样写
      

  2.   

        /**
         * Returns the text contained in this <code>TextComponent</code>. 
         * If the underlying document is <code>null</code>, will give a
         * <code>NullPointerException</code>.  For stronger
         * security, it is recommended that the returned character array be
         * cleared after use by setting each character to zero.
         *
         * @return the text
         */
        public char[] getPassword() {
            Document doc = getDocument();
    Segment txt = new Segment();
            try {
                doc.getText(0, doc.getLength(), txt); // use the non-String API
            } catch (BadLocationException e) {
        return null;
            }
    char[] retValue = new char[txt.count];
    System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
            return retValue;
        }
    以上是JPasswordField类的getPassword()的源码,看一下返回类型,还有就是如果你用IDE的话应该能看到返回类型的