建立兩個類:
一 架構
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class LibraryloginFrame extends JFrame{
public LibraryloginFrame()
{
setTitle("圖書管理系統-登陸"); //設定标題
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);//設定大小
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize =kit.getScreenSize();
double WIDTH=screenSize.getWidth();
double HEIGHT =screenSize.getHeight();
this.setBounds((int)WIDTH/2,(int)HEIGHT/2,DEFAULT_WIDTH,DEFAULT_HEIGHT);
Image ima = kit.getImage("Office2010.jpg");//設定圖示
setIconImage(ima);
//設定觀感
String plaf = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
try {
UIManager.setLookAndFeel(plaf);
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InstantiationException e2) {
} catch (IllegalAccessException e2) {
} catch (UnsupportedLookAndFeelException e2) {
}
SwingUtilities.updateComponentTreeUI(this);
File file = new File("F:\\密碼.txt");//打開儲存的密碼
FileReader reader = new FileReader(file);
BufferedReader bf = new BufferedReader(reader);
reader.close();
bf.close();
password2 = bf.readLine();
} catch (IOException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
final JTextField textField = new JTextField();
final JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(2,2));
JLabel left1 = new JLabel("User name:",SwingConstants.CENTER);
JLabel left2 = new JLabel("Password:",SwingConstants.CENTER);
northPanel.add(left1);
northPanel.add(textField);
northPanel.add(left2);
northPanel.add(passwordField);
BorderLayout bl = new BorderLayout();
this.setLayout(bl);
add(northPanel,BorderLayout.NORTH);
field = new JTextField(30);
this.add(field,BorderLayout.CENTER);
JPanel southPanel = new JPanel();
add(southPanel,BorderLayout.SOUTH);
JButton button = new JButton("确定");
southPanel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
text= textField.getText().trim();
password1 = new String(passwordField.getPassword());
result =text+password1;
if(result.equals(password2))//驗證使用者名和密碼
field.setText("恭喜你,登陸成功!!") ;
}else {
field.setText("對不起,賬号或密碼錯誤,請重新輸入!!") ;
textField.setText("");
passwordField.setText("");
}
}});
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT =200;
private JTextField field;//顯示登陸結果
private String text;//放使用者名
private String password1;//放登陸密碼
private String result;//放使用者名+密碼
private String password2;//放從密碼檔案裡讀取的密碼
二 運作類
import java.awt.EventQueue;
public class LibraryloginText {
public static void main(String[] args)
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable(){
public void run()
LibraryloginFrame frame = new LibraryloginFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
————————————————
版權聲明:本文為CSDN部落客「明明如月學長」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:
https://blog.csdn.net/w605283073/article/details/46572389