我通过swing制作登录的java程序,但是它会抛出运行时错误 [英] I made java program of login by swing but it throw run time error
本文介绍了我通过swing制作登录的java程序,但是它会抛出运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
coad如下:
import java.awt。*;
import java.awt.event。*;
import javax.swing。*;
class lg extends Frame实现ActionListener
{
JLabel l1,l2,l3;
JTextField t;
JPasswordField p;
JButton b;
lg()
{
l1 = new JLabel(user id); l1.setBounds(50,100,100,50);
t = new JTextField(); t.setBounds(100,100,100,50);
l2 = new JLabel(pass.wd); l2.setBounds(50,180,100,50);
p = new JPasswordField(); p.setBounds(100,180,100,50);
b =新JButton(登录); b.setBounds(150,250,100,50); b.setBackground(Color.green);
l3 = new JLabel(); l3.setBounds(150,330,200,50);
b.addActionListener(this);
add(l1); add(l2); add(t); add(p); add(b); add(l3);
setSize(500,500);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String u = t.getText();
char c [] = p.getPassword();
String w = String.valueOf(c);
int i = Integer.parseInt(w);
System.out.println(p);
if(e.getSource()== b)
{
String v = new String(snks);
if(u == v&& i == 1234)
System.out.println(match);
else
System.out.println(not match);
}
}
public static void main(String ... s)
{
new lg();
}
}
我的尝试:
密码匹配但用户ID不匹配抛出运行时错误
解决方案
当你提出与错误有关的问题时,还请发布错误。
如果w不是数字,包含空格或其他char parseInt将抛出异常。使用try catch包装该调用。你仍然有验证用户的问题。
尝试
如果(u .compareTo(v)== 0 ){ // 在实际情况中,u == v将不起作用。因为==将比较对象
....
}
分配文字字符串你可以简单地写
v = 你想要的常数;
请在业余时间查看这段代码,你会发现一些有趣的东西。
public static void main( String [] args) throws IOException,InterruptedException {
String x = 000;
字符串 y = new 字符串( 000);
字符串 z = 000跨度>;
System.out.println(x == y); // false
System.out.println(x == z); // true
System.out.println(x.compareTo(y)== 0 ); // true
System.out.println(x.equals(y)); // true
}
the coad is give as follow
import java.awt.*; import java.awt.event.*; import javax.swing.*; class lg extends Frame implements ActionListener { JLabel l1,l2,l3; JTextField t; JPasswordField p; JButton b; lg() { l1=new JLabel("user id");l1.setBounds(50,100,100,50); t=new JTextField();t.setBounds(100,100,100,50); l2=new JLabel("pass. wd");l2.setBounds(50,180,100,50); p=new JPasswordField();p.setBounds(100,180,100,50); b=new JButton("login");b.setBounds(150,250,100,50);b.setBackground(Color.green); l3=new JLabel();l3.setBounds(150,330,200,50); b.addActionListener(this); add(l1);add(l2);add(t);add(p);add(b);add(l3); setSize(500,500); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e) { String u=t.getText(); char c[]=p.getPassword(); String w=String.valueOf(c); int i=Integer.parseInt(w); System.out.println(p); if(e.getSource()==b) { String v=new String("snks"); if(u==v&&i==1234) System.out.println("match"); else System.out.println("not match"); } } public static void main(String...s) { new lg(); } }
What I have tried:
the password match but user id not match it throws run time error
解决方案
when you ask a question related to error, please also post the error.
if w is not number, contains space or other char parseInt will throw exception. wrap that call with try catch. you still have issue with validating user.
try
if(u.compareTo(v) == 0) { // u==v will not work, in practical cases. Since == will compare object .... }
To assign literal string you can simply write
v = "your desired constant";
Please examine this code on your spare time you will find something interesting.
public static void main(String[] args) throws IOException, InterruptedException { String x="000"; String y = new String("000"); String z = "000"; System.out.println(x==y); // false System.out.println(x==z); // true System.out.println(x.compareTo(y)==0); // true System.out.println(x.equals(y)); // true }
这篇关于我通过swing制作登录的java程序,但是它会抛出运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文