您的位置:首页 > 其它

盒子布局案例**登录页面login

2016-12-16 19:28 399 查看
打卡第二天


package com.cissst;

import java.awt.Button;

import java.awt.Dimension;

import java.awt.Toolkit;

import javax.swing.Box;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class Login extends JFrame {//盒子布局模式
private Box box1=Box.createHorizontalBox();
private Box box2=Box.createHorizontalBox();
private Box box3=Box.createHorizontalBox();
private Box box4=Box.createHorizontalBox();
private Box box5=Box.createVerticalBox();      //四个横着的盒子加到一个竖着的盒子里

private Icon icon=null;                                                                                                                   (1)继承类里面定义图片               

    private JLabel down=new JLabel();

    private JLabel name=new JLabel("登 录:");

    private JTextField myname=new JTextField();

    private JLabel  pass=new JLabel("密 码:");

    private JPasswordField mypass=new JPasswordField(25);

    private Button but1=new Button("登录");

    private Button but2=new Button("注册");

    

    public void init()

    {

    icon=new ImageIcon(this.getClass().getResource("/com/res/11.png"));                                (2)new一个image,,调用 (注意:在图片放在src下的com.ciss下的com.res                                                                    
                                                                                                                下,但是写成/com/res/11.png)

    down.setIcon(icon);                                                                                                                    (3)将图片放到容器中,,此处是放入JLable 中

    box1.add(Box.createVerticalStrut(20));

    box1.add(Box.createHorizontalStrut(20));

       

    box1.add(name);

      

    

    box1.add(myname);

        box1.add(Box.createHorizontalStrut(20));//添加横间距

   

    box2.add(Box.createHorizontalStrut(20));

    box2.add(pass);

    

    box2.add(mypass);

      box2.add(Box.createHorizontalStrut(20));

    box3.add(Box.createHorizontalStrut(20));

    box3.add(but1);

    box3.add(Box.createHorizontalStrut(20));

    box3.add(but2);

    box3.add(Box.createHorizontalStrut(20));

    box4.add(down);

    box5.add(Box.createVerticalStrut(3));

    box5.add(box1);

    box5.add(Box.createVerticalStrut(6));

    box5.add(box2);

    box5.add(Box.createVerticalStrut(6));//添加纵间距

    box5.add(box3);

   

    box5.add(box4);

    

    this.add(box5);

   

    }

    

    public Login()
{
 super("login");
 init();                                                                                                                      //将各个部件加入到布局中是在构造函数里面,,此处只是放在init()中调用而已
 
Toolkit tool=Toolkit.getDefaultToolkit();//获取屏幕的大小
Dimension dim=tool.getScreenSize();//获取屏幕的大小
int width=(int)dim.getWidth();
int height=(int)dim.getHeight();
this.setSize(230,200);
 this.setLocation((width-230)/2,(height-200)/2);
 
 
 this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
}
public static void main(String[] args) {
// TODO Auto-generated method stub

    new Login();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: