Cet article partage le code d'implémentation spécifique de l'interface de connexion Java pour votre référence. Le contenu spécifique est le suivant
1. Login.java
Package WZB; Importer java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.panel; import java.awt.event.actionevent; import java.awt.event.actionListener; import java.awt.event.keent; import java.util.random; import javax.swing; import; javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jLabel; import javax.swing.joptionpane; import javax.swing.jpasswordfield; import javax.swing.jtextfield; La classe publique Login étend JFrame implémente ActionListener {String Username; Mot de passe de chaîne; Chaîne captcha; String statique publique RandomCaptCha; Public JLabel Logolabel, UsernameLabel, PasswordLabel, Captchalabel; public JTextfield UsernameInput, captchainput; public JPasswordField PasswordInput; Public Jbutton Connexion, déconnexion, changement; panel public panel; Public Login () {SettLitle ("µçâ¼½ dese"); setSize (400, 300); setLocationRelativeto (null); init (); setVisible (true); setDefaultCloseOperation (jframe.exit_on_close); setResizable (false); } public void init () {setLayout (null); // logoLabel = new JLabel (); // logolabel.seticon (new ImageIcon ("e: //eclipse//student3//welcome.gif")); logoLabel = new JLabel (new ImageIcon ("bienvenue.gif")); Logolabel.setbounds (125, 10, 150, 70); ajouter (logoLabel); useramelabel = new Jlabel ("Óã» §ãû: "); userNameLabel.setbounds (90, 90, 60, 40); ajouter (userNameLabel); userNameInput = new JTextField (); usernameInput.setbounds (150, 100, 150, 20); ajouter (usernameInput); PasswordLabel = new JLabel ("ãü¡ ¡Âë:"); PasswordLabel.SetBounds (90, 120, 60, 40); Add (PasswordLabel); PasswordInput = new JPasswordField (); PasswordInput.SetBounds (150, 130, 150, 20); ajouter (mot de passe-mot); captchalabel = new Jlabel ("ñéö¤âë:"); capchalabel.setbounds (90, 150, 60, 40); ajouter (captchalabel); captchainput = new JTextField (); captchainput.setbounds (150, 160, 70, 20); ajouter (captchainput); Panel = new PanelDemo (); Panel.SetBounds (220, 160, 80, 20); ajouter (panneau); Change = new Jbutton ("» »ò» »» »); Change.SetBounds (300, 160, 80, 20); Change.SetContentAreafillad (false); change.setBorderPainted (false); ajouter (changer); Login = new JBUTTON ("µçâ¼ £ ¨l £ ©", New ImageIcon ("Login.gif")); Login.SetBounds (70, 200, 120, 30); login.setmnemonic (keyevent.vk_l); ajouter (connexion); Logout = new Jbutton ("íë³ö £ ¨x £ ©", new ImageIcon ("exit.gif")); Logout.SetBounds (210, 200, 120, 30); logout.setmnemonic (keyevent.vk_x); ajouter (déconnexion); userNameInput.addactionListener (this); PasswordInput.AddactionListener (this); capchainput.addactionListener (this); login.addactionListener (this); logout.addactionListener (this); change.addactionListener (this); } public void ActionPerformed (actionEvent e) {username = usernameInput.getText (); mot de passe = new String (PasswordInput.GetPassword ()); captcha = captchainput.getText (); if (e.getSource () == Change) {Panel.Repaint (); } if (e.getsource () == connexion) {if ((username.equals ("w")) && (mot de passe.equals ("w")) {if (captcha.equals (randomcaptcha)) {joptionpane.showMessageDialog (this, "» ¶óµçâ! "); } else {joptionpane.showMessagealialog (this, "ñéö¤âë´íîó!"); panneau.Repaint (); }} else {joptionpane.showMessagealialog (this, "óã» §ãû »òãüâë´íîó!"); }} if (e.getsource () == Logout) {joptionpane.showMessageDialog (this, "» ¶óïâ´îôùà´ £ ¡"); //System.exit(0); disposer(); }} public static void main (String [] args) {new Login (); }} classe PanelDemo étend le panneau {public void peinture (graphiques g) {int width = 80; int hauteur = 20; g.setColor (Color.light_Gray); G.Fillrect (0, 0, largeur, hauteur); g.setColor (color.black); G.Drawrect (0, 0, largeur, hauteur); Random rd = new Random (); pour (int i = 0; i <100; i ++) {int x = rd.nextint (largeur) - 2; int y = rd.nextint (hauteur) - 2; g.setColor (Color.Red); G.Drawoval (x, y, 2, 2); } g.setfont (nouvelle police ("ºúìå", font.bold, 20)); g.setColor (Color.Blue); char [] c = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789" .tocharay (); StringBuffer sb = new StringBuffer (); pour (int i = 0; i <4; i ++) {int index = rd.nextint (c.Length); sb.append (c [index] + ""); } g.Drawstring (sb.toString (), 0, 18); String str = sb.toString (). RempaceALL ("", ""); Login.randomcaptcha = str; }}2. capture.png
Ce qui précède concerne cet article, j'espère qu'il sera utile pour tout le monde d'apprendre la programmation Java.