package zzDemo; import java.awt.Button; import java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JOptionPane; public class QQlogin { private static Frame f; private static TextField zh; private static TextField mm; public static void main(String[] args) { f = new Frame(); f.setSize(300, 200); Int x= setLocation.getx (f); int y=setLocation.getY(f); f.setLocation(x, y); // Form centered f.layout (null); // Clear the form style f.stage (false); / / prohibited form change font size (display) f.s etIconImage (Toolkit. GetDefaultToolkit (). The getImage (" img/qq_3d. PNG ")); f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { f.dispose(); // Listener closed}}); // Label name = new Label(" Label "); name.setSize(50, 30); name.setLocation(30, 50); f.add(name); PWD = new Label(" password: "); pwd.setSize(50, 30); pwd.setLocation(30, 80); f.add(pwd); // Account TextField zh = new TextField(); zh.setSize(150, 20); zh.setLocation(80, 55); // Add a listening event for keyboard input, AddKeyListener (new KeyListener() {@suppressWarnings ("static-access") @override public void en. addKeyListener(new KeyListener() {@suppressWarnings ("static-access") @override public void KeyTyped (KeyEvent e) {// Int key= LLDB keychar (); If (key > = e.V K_0 && key < = e.V K_9) {} else if (key = = e.V K_ENTER) {/ / if press enter key to check (); }else{ e.consume(); }} @override public void keyPressed(KeyEvent e) {// TODO auto-generated method stub} @override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }); f.add(zh); // Password box mm = new TextField(); mm.setSize(150, 20); mm.setLocation(80, 85); mm.setEchoChar('*'); AddKeyListener (new KeyListener() {@suppressWarnings ("static-access") @override public void addKeyListener(new KeyListener() {@suppressWarnings ("static-access") @override public void keyTyped(KeyEvent e) { int key=e.getKeyChar(); if(key==e.VK_ENTER){ check(); } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } }); f.add(mm); // Button login = new Button(); The login. SetLabel (" login "); login.setSize(100, 30); login.setLocation(30, 130); AddActionListener (new ActionListener() {@override public void actionPerformed(ActionEvent arg0) { check(); }}); f.add(login); // Register Button reg = new Button(" register "); reg.setSize(100, 30); reg.setLocation(140, 130); reg.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String Mess = JOptionPane. ShowInputDialog (" please enter: "); System.out.println(mess); }}); f.add(reg); f.setVisible(true); f.setResizable(false); } public static void check(){// Get the password. String t1= zh.gettext (); String t2= mm.gettext (); / / password information if (t1) equals (" 112233 ") && t2. Equals (" 1111 ")) {/ / normal messages prompt box / / JOptionPane showMessageDialog (f, "login successfully! About to jump to the QQ home page..." ); / / with a choice message prompt box int I = JOptionPane. ShowConfirmDialog (f, "open the home page?" ); If (I ==0){//0 represents yes f.dispose(); // Close the login interface new Main(); }else{ } }else if(t1.equals("112233") && ! T2. Equals (" 1111 ")) {JOptionPane. ShowInternalMessageDialog (f, "password error"); mm.setText(""); Mm.requestfocus (); / / get the current focus, the cursor stops here} else if (t1) equals (" ") | | t1 = = null) {JOptionPane. ShowMessageDialog (f, "account can't be empty"); zh.requestFocus(); } else if (t2) equals (" ")) {JOptionPane. ShowMessageDialog (f, "password cannot be empty"); mm.requestFocus(); } else {JOptionPane. ShowMessageDialog (f, "account or password error"); zh.setText(""); mm.setText(""); Zh.requestfocus (); }}}Copy the code
package zzDemo; import java.awt.Frame; import java.awt.Toolkit; /** * Form center method: * @author Administrator * */ public class setLocation {public static int getX(Frame f){int width=(int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); int x=(int) f.getSize().getWidth(); Return (width-x)/2; Public static int getY(Frame f){public static int getY(Frame f){ Toolkit.getDefaultToolkit().getScreenSize().getHeight(); int y=f.getSize().height; return (height-y)/2; }}Copy the code
package zzDemo; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Main { public Main(){ Frame f=new Frame(); f.setSize(500, 800); int x=setLocation.getX(f); int y=setLocation.getY(f); f.setLocation(x, y); f.setVisible(true); f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { f.dispose(); }}); }}Copy the code