java 代码

[复制链接]
查看11 | 回复2 | 2010-8-17 15:15:37 | 显示全部楼层 |阅读模式
小弟写了段java代码,但是很奇怪,当我在用户名文件框内输入lucy时,还是提示登录失败。但是我的name变量显示出来是lucy。不知道哪里写错了,求高人指点!
logonbutton = new Button(group, SWT.NONE);
logonbutton.setBounds(new Rectangle(31, 78, 51, 23));
logonbutton.setText("登录");
logonbutton.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {

System.out.println("mouseDown()"); // TODO Auto-generated Event stub mouseDown()

String name = usertext.getText();

String password = passwordtext.getText();

if(name == "lucy")

display.setText("登录成功!");

else

display.setText("登录失败!\n正确的名字是:" + name);

}
});

回复

使用道具 举报

千问 | 2010-8-17 15:15:37 | 显示全部楼层
IF里面(name == "lucy")你改成(name.equals("lucy"))试试
回复

使用道具 举报

千问 | 2010-8-17 15:15:37 | 显示全部楼层
额 字符串比较不能用==应该用equals方法 string 对象是一个引用对象==是对值对象进行比较
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行