A simple login interface

2016.12.18

 

I talked about the login interface of java before, but you know that many things have not been blogged, so it is a matter of course and sadly forgotten, so now it is necessary to consolidate it.

One: Graphical interface: AWT

(1) AWT

1. The components of AWT are drawn by the drawing mechanism of the operating system;

2. The components of AWT are under the java.awt package.

(2)SWING

1. The components of SWING are under the javax, swing package;

2. The SWING component is a new component refactored on the basis of AWT.

(2) Commonly used component classes

Container component: A container or element component that can be added is a container component class

 JFrame form container component class top-level container

Element component: generally used to display text, pictures, and the component that accepts input is the element component class

 

   JLabel   

    Label element component class     Component class used to display pictures, questions
    JTextField        Text input box component class      Used to accept the input information and display it directly
    JPasswordField     Password input box component class   Used to receive input, but instead display it with a symbol
    JCheckBox        Checkbox element component class     Has a selection box that can also display text or images 
   JButton  Button element component class  Used to display pictures or text, and click    

 1. Auxiliary class: a class that helps a component complete a certain function

  FlowLayout flow layout class is similar to word document

  Dimension class that encapsulates width and height

  ImageIcon icon class loads an image into the program

  FlowLayout and Dimension are under the java.awt package

  ImageIcon is in the javax.swing package.

 

  Steps to implement a login interface

      

  1. Create a new Login.java class, and then define the main function and the method of initializing the interface in the class.   
  2. In the main function, an object of the Login class is instantiated, and the method of initializing the interface is called. 
  3. Instantiate a JFrame top-level container form object, set the property values ​​of the form object: size, title, display position, layout, close, visible
  4.  Create an object of the component class and add the component to the form.
  5. 2. Interface:

    (1) Interface (format)

    • Definition of an interface
    • Keyword to define an interface: interface
    • Format:
    • public interface interface name extends interface,... {
    • //Define constants (constant names must be all uppercase)
    • public static final data type constant name = value;
    • //define abstract method
    • public abstract return value type method name (data type parameter name,...); }

    Note:
    1. The interface has only one access modifier public.
    2. The keywords provided by the interface by default are: public, static, final, abstract
    3. The interface cannot instantiate objects.

  6. 2. The class implements the interface (the class inherits the interface)

    Class inheritance is single inheritance. With interfaces, classes can inherit multiple interfaces (rich class inheritance).
    Keywords for implementing 
    the interface: implements Format of implementing the interface:
    public class class name extends class name implements interface name,... {

    //All abstract methods in the interface must be implemented (the methods in the parent interface of the interface must also be implemented )

    }
    Note: Subclasses must implement all abstract methods in the interface.

    3. Note the difference between interface and class

    2.事件机制(重点)
    1.事件源对象
    1.有哪些内容可以成为事件源对象?
    所有的组件都可以成为事件源对象。
    2.在界面上你如何确定谁是事件源对象呢?
    你在哪一个组件上发生动作,那么这个组件就是你的事件源对象。

    2.事件监听方法
    addActionListener(ActionListener l);
    动作监听方法,该方法主要用来监听是否在类似按钮事件源对象上发生鼠标点击动作或者
    在类似输入框事件源对象上发生键盘的回车动作;如果监听到动作后,就会交给参数
    ActionListener的事件处理类的对象进行处理,对象就会自动调用事件处理方法。

    addMouseListener(MouseListener l);
    鼠标监听方法,该方法主要用来监听事件源对象上是否有鼠标按下、释放、单击、进入
    和离开动作;如果监听到动作后,就会交给参数MouseListener的事件处理类的
    对象进行处理,对象就会自动调用事件处理方法。

    addMouseMotionListener(MouseMotionListener l);
    鼠标移动监听方法,该方法主要用来监听事件源对象上是否有鼠标移动或者拖动动作;
    如果监听到动作后,就会交给参数MouseMotionListener的事件处理类的对
    象进行处理,对象就会自动调用事件处理方法。

    addKeyListener(KeyListener l);
    键盘监听方法,该方法主要用来监听事件源对象上是否有键盘按键按下,释放和敲击动作;
    如果监听到动作后,就会交给参数KeyListener的事件处理类的对
    象进行处理,对象就会自动调用事件处理方法。

    三.事件接口(事件处理类)
    ActionListener 动作事件接口
    MouseListener 鼠标事件接口
    MouseMotionListener 鼠标移动事件接口
    KeyListener 键盘事件接口

    接口是否能实例化对象呢?不可以
    定义事件处理类实现事件接口

    4.事件执行流程

    目标:点击登录界面上的登录按钮,就要显示一个新的界面。
    1.事件源对象:登录按钮
    2.事件监听方法:addActionListener(ActionListener l);
    3.事件接口(事件处理类):ActionListener

    开发步骤:
    1.定义LoginListener事件处理类,该类实现ActionListener动作事件接口,实现接口中的抽象方法。
    2.在事件处理方法(实现的抽象方法)中,显示一个新的界面
    3.在界面类中,实例化LoginListener事件处理类的对象ll.
    4.改事件源butLogin添加动作监听方法,指定事件处理类的对象ll.

    登录成功后,登录界面就不需要再显示了。
    事件处理类中没有登录界面窗体对象,所需你需要定义方法向登录界面类去借窗体对象。

  7. package DemoDengLu;
    
    import java.awt.Dimension;
    
    public class login {
    //1.程序的入口主函数
    public static void main(String[] args){
    //在主函数中,实例化login类的对象,调用初始化界面的方法。
       login l=new login();
       l.initUT();
    }
    //1.设置登入界面的方法。
    public void initUT(){
       //public 返回值类型 方法名(数据类型 参数名){}
       //实例化一个JFrame顶级容器船体对象
       javax.swing.JFrame jf=new javax.swing.JFrame();
       jf.setSize(300,470);//设置的大小。单位是像素
       jf.setResizable(false);//设置为不可以调整大小。
       jf.setTitle("简单登录界面");//设置窗体的标题。
       jf.setLocationRelativeTo(null);//设置窗体的显示的位置,null表示在中央
       jf.setDefaultCloseOperation(3);//设置关闭窗口时,3关闭窗口退出程序
       //实例化一个流式布局类的对象,布局类是针对容器的,容器上要填多个组件,那么必须要设置排列对齐方式
       java.awt.FlowLayout fl=new java.awt.FlowLayout();
       jf.setLayout(fl);//设置窗体的布局方式为流式布局
       //定义一个ImageIcon类,该类用来读取一个磁盘的图片文文件。
       javax.swing.ImageIcon image=new javax.swing.ImageIcon("C:\\Users\\tuocheng\\Desktop\\庹成.png");
       //创建一个JLable类的对象,用来显示加载的图片
       javax.swing.JLabel labelImage=new javax.swing.JLabel(image);
       jf.add(labelImage); //将LabelImage添加到窗体上
       //实例化一个JTextField类的对象
       javax.swing.JTextField textName=new javax.swing.JTextField();
       Dimension dim= new java.awt.Dimension(200,30); //实例化一个封装组件高度和高度
       textName.setPreferredSize( dim);//將textName添加在窗体上
       jf.add(textName);
    
       javax.swing.JLabel labelShopping=new javax.swing.JLabel("注册登入");
       jf.add(labelShopping);
    
       javax.swing.JPasswordField Passwordname=new javax.swing.JPasswordField();
       java.awt.Dimension dis= new java.awt.Dimension(200,30);
       Passwordname.setPreferredSize(dis);
       jf.add(Passwordname);
    
       javax.swing.JLabel labelPassword=new javax.swing.JLabel("登入密码");
       jf.add(labelPassword);
    
       javax.swing.JCheckBox cbl=new javax.swing.JCheckBox("记住密码 ");
       java.awt.Dimension dic=new java.awt.Dimension(130,30);
       cbl.setPreferredSize(dic);
    
       jf.add(cbl);
       javax.swing.JCheckBox cba=new javax.swing.JCheckBox("找回密码 ");
       java.awt.Dimension dib=new java.awt.Dimension(130,30);
       cba.setPreferredSize(dib);
    
       jf.add(cba);
       javax.swing.JButton cbc=new javax.swing.JButton("登入 ");
       java.awt.Dimension div=new java.awt.Dimension(200,30);
       cbc.setPreferredSize(div);
       jf.add(cbc);
       jf.setVisible(true);//设置窗体可见
    
       LoginListener ll=new LoginListener();
       cbc.addActionListener(ll);//添加监听方法。
    
       ll.setloginFrame(jf);
       ll.setText(textName);
       ll.setEchoChar(Passwordname);
    
      }
    }
    
     监听器类: 
  8. package DemoDengLu;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    //ctrl+shift+o,自动生成。
    //
    public class LoginListener implements ActionListener {
    //ActionListener系统自带的接口
    /**
    * 用一个函数来把login的窗口以及值传过来。
    */
    public JFrame login;//public 数据类型 属性名;
    public JTextField textName;
    public JPasswordField Passwordname;
    
    public void setloginFrame(JFrame l) {
        login = l;
    }//public 返回值类型 方法名(数据类型 参数名){}
    
    public void setText(JTextField s) {
       textName = s;
    }
    
    public void setEchoChar(JPasswordField v) {
        Passwordname = v;
    }
    
    public void actionPerformed(ActionEvent e) {
       String t = textName.getText();
       String str = new String(Passwordname.getPassword());
    if (t.equals("tuocheng") && str.equals("12345")) {
       //参数名.equals相当于==。
       JFrame frame = new JFrame();
       frame.setTitle("欢迎来到新界面");
       frame.setSize(600, 500);
       frame.setDefaultCloseOperation(3);
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
    
       login.dispose();//关闭登入界面。
       } 
       else {
       JFrame frame = new JFrame();
       frame.setTitle("错误提示");
       frame.setSize(200, 100);
       frame.setDefaultCloseOperation(3);
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
     }
    
     }
    
    }
     

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326789797&siteId=291194637