首页上一页 1 下一页尾页 3 条记录 1/1页
运行成功无异常,但是单击按钮就出现了异常!
发表在Java图书答疑
2016-08-28
是否精华
是
否
版块置顶:
是
否
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class DengRu extends JFrame{
/**
*
*/
private static final long serialVersionUID = 7691130959765358907L;
TextField jt;
TextField jt2;
JButton jb1;
JButton jb2;
JButton jb3;
String power;
public DengRu(){
setTitle("登入");
Container C=getContentPane();
getContentPane().setLayout(new GridLayout(4,1));
JPanel p=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JLabel jl=new JLabel("账户:");
JLabel jl2=new JLabel("密码:");
JTextField jt=new JTextField(10);
JPasswordField jt2=new JPasswordField(10);
jt2.setEchoChar('*');
JButton jb=new JButton("登入");
JButton jb2=new JButton("重置");
JButton jb3=new JButton("退出");
p.add(jl);
p.add(jt);
p2.add(jl2);
p2.add(jt2);
p3.add(jb);
p3.add(jb2);
p3.add(jb3);
C.add(p);
C.add(p2);
C.add(p3);
jb2.addActionListener(new Enter());
setSize(250,180);
setVisible(true);
setBackground(Color.blue);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt.setText("请输入");
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt.setText("");
jt2.setText("");
jt.requestFocus();
jt2.requestFocus();
}
});
}
public static void main(String[] args){
new DengRu();
}
class Enter implements ActionListener{
public void actionPerformed(ActionEvent e) {
if ((jt.getText()).equals("zwz") && (jt2.getText()).equals("zwz123")) {
JOptionPane.showMessageDialog(null, "登录成功!用户权限是管理员");
DengRu.this.setVisible(true);
power = "管理员";
} else if ((jt.getText()).equals("abc") && (jt2.getText()).equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功!登录成功!用户权限是游客");
DengRu.this.setVisible(true);
power = "游客";
} else
JOptionPane.showMessageDialog(null, "登录失败,请重新登录!");
}
}
}
(ps:运行的时候无异常,也没出现异常信息。出现异常后,重置按钮也可以照常操作,当我吧“Enter”的接口去掉后,就没有出现异常了,重置按钮也可以照常操作)
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class DengRu extends JFrame{
/**
*
*/
private static final long serialVersionUID = 7691130959765358907L;
TextField jt;
TextField jt2;
JButton jb1;
JButton jb2;
JButton jb3;
String power;
public DengRu(){
setTitle("登入");
Container C=getContentPane();
getContentPane().setLayout(new GridLayout(4,1));
JPanel p=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JLabel jl=new JLabel("账户:");
JLabel jl2=new JLabel("密码:");
JTextField jt=new JTextField(10);
JPasswordField jt2=new JPasswordField(10);
jt2.setEchoChar('*');
JButton jb=new JButton("登入");
JButton jb2=new JButton("重置");
JButton jb3=new JButton("退出");
p.add(jl);
p.add(jt);
p2.add(jl2);
p2.add(jt2);
p3.add(jb);
p3.add(jb2);
p3.add(jb3);
C.add(p);
C.add(p2);
C.add(p3);
jb2.addActionListener(new Enter());
setSize(250,180);
setVisible(true);
setBackground(Color.blue);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt.setText("请输入");
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt.setText("");
jt2.setText("");
jt.requestFocus();
jt2.requestFocus();
}
});
}
public static void main(String[] args){
new DengRu();
}
class Enter implements ActionListener{
public void actionPerformed(ActionEvent e) {
if ((jt.getText()).equals("zwz") && (jt2.getText()).equals("zwz123")) {
JOptionPane.showMessageDialog(null, "登录成功!用户权限是管理员");
DengRu.this.setVisible(true);
power = "管理员";
} else if ((jt.getText()).equals("abc") && (jt2.getText()).equals("123456")) {
JOptionPane.showMessageDialog(null, "登录成功!登录成功!用户权限是游客");
DengRu.this.setVisible(true);
power = "游客";
} else
JOptionPane.showMessageDialog(null, "登录失败,请重新登录!");
}
}
}
(ps:运行的时候无异常,也没出现异常信息。出现异常后,重置按钮也可以照常操作,当我吧“Enter”的接口去掉后,就没有出现异常了,重置按钮也可以照常操作)