JAVA-继承

image
image
image
image
image

package com.itheima04;

import javax.swing.*;

public class UserLoginFrame extends JFrame {

    public UserLoginFrame() {
        //窗体初始化
        initframe();


        //绘制窗体
        paintView();
        
        //添加按钮到窗体中
        this.setVisible(true);


    }


    public void initframe() {
        this.setTitle("用户登录");
        this.setSize(400, 300);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);        //取消窗体的默认布局
    }

    public void paintView() {
        //显示用户名文本
        JLabel usernameLabel = new JLabel("用户名");
        usernameLabel.setBounds(50, 50, 50, 20);
        this.add(usernameLabel);

        //用户名输入框
        JTextField usernameField = new JTextField();
        usernameField.setBounds(150, 50, 180, 20);
        this.add(usernameField);

        //显示密码文本
        JLabel passwordLabel = new JLabel("密码");
        passwordLabel.setBounds(50, 100, 50, 20);
        this.add(passwordLabel);

        JPasswordField passwordField = new JPasswordField();  //密码输入框用JPasswordField 加密显示
        passwordField.setBounds(150, 100, 180, 20);
        this.add(passwordField);

        //登录按钮
        JButton LoginButton = new JButton("登录");
        LoginButton.setBounds(50, 200, 280, 20);
        this.add(LoginButton);

    }

}

代码2

package com.itheima04;

public class App {
    public static void main(String[] args) {
        UserLoginFrame userloginframe = new UserLoginFrame();

    }
}

执行结果

image

posted @ 2022-11-14 22:33  NiceTwocu  阅读(11)  评论(0编辑  收藏  举报