導入Java . awt . event . *;
導入javax . swing . *;
公共類Test_Login擴展javax.swing.JFrame {
私有JPanel JPanel 1;
私有JButton bntLogin
私有JButton bntCannel
私有JPasswordField pwd
私有JTextField用戶名;
二等兵JLabel jLabel2
private JLabel JLabel 1;
公共靜態void main(String[] args) {
Test _ log in inst = new Test _ log in();
inst . setlocationrelativeto(null);
inst . set visible(true);
}
public Test_Login() {
super();
init GUI();
}
私有void initGUI() {
嘗試{
setDefaultCloseOperation(window constants。DISPOSE _ ON _ CLOSE);
{
JPanel 1 = new JPanel();
getContentPane()。add(jPanel1,BorderLayout。中心);
jpanel 1 . set layout(null);
{
JLabel 1 = new JLabel();
jpanel 1 . add(jlabel 1);
JLabel1.setText("用戶名");
jLabel1.setBounds(45,30,75,25);
}
{
JLabel 2 = new JLabel();
jpanel 1 . add(jlabel 2);
jlabel 2 . settext(" password ");
jLabel2.setBounds(45,75,55,15);
}
{
username = new JTextField();
jPanel1.add(用戶名);
username.setBounds(100,30,140,25);
}
{
pwd = new JPasswordField();
jpanel 1 . add(pwd);
pwd.setBounds(100,70,140,25);
}
{
bnt log in = new JButton();
jpanel 1 . add(bnt login);
bnt log in . settext(" log in ");
bntLogin.setBounds(40,120,60,30);
bntLogin.addMouseListener(新的MouseAdapter() {
public void mouse clicked(mouse event e){
If (username.getText()。等於(“小王”)
& amp& amppwd.getText()。等於("王曉"){
joptionpane . showmessagedialog(Test _ log in . this,
“登錄成功”);
}否則{
joptionpane . showmessagedialog(Test _ log in . this,
“登錄失敗”);
}
}
});
bnt channel = new JButton();
jpanel 1 . add(bnt channel);
bnt channel . settext(" cancel ");
bnt channel . set bounds(180,120,60,30);
bnt channel . addmouse listener(新的MouseAdapter() {
public void mouse clicked(mouse event e){
system . exit(0);
}
});
}
}
pack();
setSize(300,215);
} catch(異常e) {
e . printstacktrace();
}
}
}
嘗試使用此代碼在用戶登錄界面中輸入用戶名和密碼。