打卡第二天
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;
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")); //图片放在src 下的com.cissst下的com.res下的11.png注意些成了"/com/res/11.png"
down.setIcon(icon);
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();
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();
}
}