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 秒) - 正常终止
解决方案 »
- hibernate 映射sql server中的binary字段做主键
- 我载了Borland JBuilder2005企业版CD1.bin 和Borland JBuilder2005企业版CD2.bin,怎么安装?
- 一个简单的类,看不懂,麻烦讲解一下(300分,重信誉,必结贴)
- 昨天面试的时候遇见的一个问题
- 妹妹的20个作业之二
- 如何在一个JFrame上面设置背景图片
- 线程死锁
- 如何使用Runtime.getRuntime.exec()函数实现 mysql的数据库操作命令:mysql db < db.sql
- 谁有Jbuilder7.0的serial number和key
- 请问 学java的数据库编程,有什么书比较好?
- JSTL该如何配置?
- 关于JAVA中创建临时数据库的问题
或 String password =new String(textCustPassword.getPassword());
这样写
* 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的话应该能看到返回类型的