仿QQ登录界面

前段时间学习了关于登录界面的代码编写,觉得挺有意思,开始的时候一直接纠结于窗体上一些组件的排版问题,每次写完代码后总发现那些组件的排列顺序总不能想自己想的那样,后来只有好好的钻研几种布局的特点及用法,还有一些其他容器的用法,解决一些复杂结构的排版,就要用到JPanel和JSplitPane,但是用的还是很熟练,以下是我根据资料写的一个仿QQ登录界面,也是这几天研究的关于在登录界面中添加背景图片,添加了背景图片以后确实很难进行复杂的结构排序,所以下面的界面很简单。

代码如下:

 

package java0627;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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 LoginJFrame {
	public JFrame jframe;
	public JLabel jlabel, jlabel1;
	public GridBagLayout gridbag;
	public GridBagConstraints constraints;
	public JTextField jtfield1;
	public  JPasswordField jpfield1;
	public JButton jbutton1, jbutton2, jbutton3;
	public JPanel jpanel;

	 /**
	 *  主函数
	 */
	public static void main(String[] args) {
		LoginJFrame lj = new LoginJFrame();
		lj.init();
		lj.showMe();
 

	}
	/**
	 * init()初始化并显示界面
	 */
	private void init(){
	 	jframe = new JFrame();
	 	jframe.setTitle("小鱼儿的QQ");
	 	jlabel = new JLabel();
	 	jlabel.setText("用户名:");
 	    jlabel1 = new JLabel();
 	    jlabel1.setText("密    码:");
	 	jtfield1 = new JTextField(7);
	 	jpfield1 = new JPasswordField(7);
	 	gridbag = new GridBagLayout();
 	    jbutton1 = new JButton();
 	    jbutton1.setText("登    录");
 	    jbutton2 = new JButton();
 		jbutton2.setText("退    出");
 	    jbutton3 = new JButton();
		jbutton3.setText("更改密码");
        jpanel = new JPanel() {
		 protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				java.net.URL ur12 = this.getClass().getResource("j01.jpg");
				ImageIcon img = new ImageIcon(ur12);
				img.paintIcon(this, g, 0, 0);
			}
		};

		//设置窗体属性
		jframe.setBounds(200, 200, 460, 340);
		jframe.setDefaultCloseOperation(3);

		// 设置JPanel为透明,且使用GridBagLayout布局管理器
		jpanel.setOpaque(true);
		jpanel.setLayout(gridbag);

		// 用户名文本框显示
		constraints = getGridBagConstraints(0, 0, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 0, 0);

		gridbag.setConstraints(jlabel, constraints);
		jpanel.add(jlabel);

		// 用户名输入框显示
		constraints = getGridBagConstraints(1, 0, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 100, 0);

		gridbag.setConstraints(jtfield1, constraints);
		jpanel.add(jtfield1);

		// 密码文本框显示
		constraints = getGridBagConstraints(0, 1, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 0, 0);
		gridbag.setConstraints(jlabel1, constraints);
		jpanel.add(jlabel1);

		// 密码输入框显示
		constraints = getGridBagConstraints(1, 1, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 100, 0);

		gridbag.setConstraints(jpfield1, constraints);
		jpanel.add(jpfield1);

		// 更改密码按钮显示
		constraints = getGridBagConstraints(0, 2, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 0, 0);

		gridbag.setConstraints(jbutton3, constraints);
		jpanel.add(jbutton3);

		// 登录按钮显示
		constraints = getGridBagConstraints(1, 2, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 0, 0);

		gridbag.setConstraints(jbutton1, constraints);
		jpanel.add(jbutton1);

		// 退出按钮显示
		constraints = getGridBagConstraints(2, 2, 1, 1, 0, 0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						10, 0, 10, 0), 0, 0);

		gridbag.setConstraints(jbutton2, constraints);
		jpanel.add(jbutton2);

		jframe.add(jpanel);
	}

	private static GridBagConstraints getGridBagConstraints(int gridx,
			int gridy, int gridwidth, int gridheight, double weightx,
			double weighty, int anchor, int fill, Insets insets, int ipadx,
			int ipady) {

		return new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
				weightx, weighty, anchor, fill, insets, ipadx, ipady);
	}

	public void showMe() {
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setVisible(true);
	}

	
}

 

 附件上有登录界面截图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值