Java_Swing程序設計_嘗試開發一個登陸窗體,包括用戶名、密碼以及提交按鈕和重置按鈕,當用戶輸入用戶名my,密碼love時,彈出登陸成功提示對話框。


package com.lzw;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class UseCase3 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;

public UseCase3(){
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle("登錄窗體");
setBounds(300, 200, 300, 150);
Container cp=getContentPane();
cp.setLayout(null);
JLabel jl=new JLabel("用戶名:");
jl.setBounds(10, 10, 200, 18);
final JTextField name=new JTextField();
name.setBounds(80, 10, 150, 18);
JLabel jl2=new JLabel("密碼:");
jl2.setBounds(10, 50, 200, 18);
final JPasswordField password=new JPasswordField();
password.setBounds(80, 50, 150, 18);
cp.add(jl);
cp.add(name);
cp.add(jl2);
cp.add(password);
JButton jb=new JButton("確定");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(name.getText().trim().length()==0||new String(password.getPassword()).trim().length()==0){
JOptionPane.showMessageDialog(null, "用戶名密碼不允許為空");
return;
}
if(name.getText().trim().equals("my")&&new String(password.getPassword()).trim().equals("love")){
JOptionPane.showMessageDialog(null, "登錄成功");
}
else{
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤");
}
}
});
jb.setBounds(80, 80, 60, 18);
cp.add(jb);

final JButton button = new JButton();
button.setText("重置");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自動生成方法存根
name.setText("");
password.setText("");
}
});
button.setBounds(150, 80, 60, 18);
getContentPane().add(button);
}

public static void main(String[] args) {
new UseCase3();

}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM