首页 > 技术文章 > Java图形界面学习---------简易登录界面

oycyqr 2015-10-31 13:29 原文

/**
 * @author Administrator
 * Java图形界面学习---------简易登录界面
 * date:2015/10/31
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class LoginDemo extends JFrame{
//定义组件
	JPanel jp1,jp2;
	JLabel Imjb,Unlb,Pwlb;
	JTextField jtf;
	JPasswordField jpw;
	JButton remJB,LoginJB,cannelJB;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LoginDemo lg=new LoginDemo();
	}
	//构造方法
	public LoginDemo(){
	  //创建组件
		//上部
		ImageIcon icon = new ImageIcon("images//Login.jpg");
		Imjb=new JLabel(icon);
		Imjb.setOpaque(false); 
		//中部
		jp1=new JPanel();
		jp1.setLayout(new GridLayout(2,1));
		Unlb=new JLabel("    用户名:");
		Unlb.setFont(new Font("楷体",Font.PLAIN,18));
		Unlb.setForeground(Color.BLUE);
		Pwlb=new JLabel("    密  码:");
		Pwlb.setFont(new Font("楷体",Font.PLAIN,18));
		Pwlb.setForeground(Color.BLUE);
		jtf=new JTextField(10);
		jpw=new JPasswordField(10);
		jp1.add(Unlb);
		jp1.add(jtf);
		jp1.add(Pwlb);
		jp1.add(jpw);
		
		//下部
		jp2=new JPanel();
		remJB=new JButton("记住密码");
		remJB.setForeground(Color.red);
		LoginJB=new JButton("登 录");
		cannelJB=new JButton("取 消");
		jp2.add(remJB);
		jp2.add(LoginJB);
		jp2.add(cannelJB);
	//添加组件
		 //设置布局方式
   	    this.setLayout(new GridLayout(3,1));
		this.add(Imjb,BorderLayout.NORTH);
		this.add(jp1);
		this.add(jp2,BorderLayout.SOUTH);
		
		//设置窗体属性
 		this.setTitle("用户登录");
		this.setIconImage(new ImageIcon("images/qq.png").getImage());
	   //设置窗口居中
	    int width=getToolkit().getDefaultToolkit().getScreenSize().width;
	    int height=getToolkit().getDefaultToolkit().getScreenSize().height;
	    this.setLocation(width/2-200,height/2-200);
		this.setSize(250,168);
		this.setResizable(false);
 		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}

  运行截图:

推荐阅读