使用绝对布局方式实现登录界面

一、写在前面

1. 使用绝对布局方式实现登录窗口;

2. 禁止窗口缩放与最大化;

二、代码

public class AbsoluteLoginFrame extends JFrame {
    private static final int LOGIN_WIDTH = 600;
    private static final int LOGIN_HEIGHT = 400;

    private static final long serialVersionUID = -2381351968820980500L;

    public AbsoluteLoginFrame(){
        //设置窗口标题
        setTitle("登录界面");

        //设置一个初始面板,填充整个窗口
        JPanel loginPanel = new JPanel();
        //设置背景颜色
        loginPanel.setBackground(new Color(204, 204, 204));//#CCC
        loginPanel.setLayout(null);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.WHITE);
        centerPanel.setBounds(114, 70, 360, 224);
        centerPanel.setLayout(null);

        JLabel jLabel = new JLabel("用户名:");
        jLabel.setOpaque(true);
        jLabel.setBackground(Color.YELLOW);
        jLabel.setBounds(60, 60, 54, 20);

        JLabel label = new JLabel("密    码:");
        label.setOpaque(true);
        label.setBackground(Color.CYAN);
        label.setBounds(60, 90, 54, 20);

        JTextField textField = new JTextField(15);
        textField.setBounds(130, 60, 166, 21);

        JPasswordField passwordField = new JPasswordField(15);
        passwordField.setBounds(130, 90, 166, 21);

        JButton jButton = new JButton("登录");
        jButton.setBounds(148, 120, 62, 28);

        centerPanel.add(jLabel);
        centerPanel.add(label);
        centerPanel.add(textField);
        centerPanel.add(jButton);
        centerPanel.add(passwordField);
        loginPanel.add(centerPanel);
        getContentPane().add(loginPanel);//将初始面板添加到窗口中

        setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//设置窗口大小
        setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置
        setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new AbsoluteLoginFrame();
    }
}
public class Screen {

    private int width;
    private int height;

    public Screen(){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        this.width = screenSize.width;
        this.height = screenSize.height;
    }

    public static Point getCenterPosition(int width, int height){
        Screen screen = new Screen();
        int x = (screen.getWidth() - width) / 2;
        int y = (screen.getHeight() - height) / 2;
        return new Point(x, y);
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

 三、效果图

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值