JavaGUI小结——实验做的QQ登录界面

GUI(Graphical User Interface) 即图形用户界面.

Java提供了三个主要包做GUI开发:

java.awt 包 – 主要提供字体/布局管理器

常见的布局方式:FlowLayout(流布局)、BorderLayout(边框布局)、
GridLayout(网格布局)

javax.swing 包[商业开发常用] – 主要提供各种组件(窗口/按钮/文本框)

java.awt.event 包 – 事件处理,后台功能的实现。

swing组件主要可分为三个部分;

1.顶层容器::常用有JFrame,JDialog
JFrame

//一个普通的窗口(绝大多数 Swing 图形界面程序使用 JFrame 作为顶层容器);

JDialog //对话框;

基本组件:JLabel,JButton,JTextField,JPasswordField,JRadioButton 等。

JLabel                       标签
JButton                     按钮
JRadioButton            单选按钮   
JCheckBox               复选框
JToggleButton          开关按钮
JTextField                文本框
JPasswordField       密码框
JTextArea                文本区域
JComboBox            下拉列表框
JList                         列表
JProgressBar          进度条
JSlider                     滑块

JLabel label = new JLabel(“Hello World”); 添加标签

JButton button = new JButton(“选择文件”); 创建按钮

button.addActionListener(new ActionListener() {} 添加操作按钮后的事件***

jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
// 设置文件对话框

jfc.showSaveDialog(frmIpa);

// 显示打开的文件对话框`
jfc.getSelectedFile();

// 获取选择器选择的文件

JTextArea textArea = new JTextArea();

创建文本区域

textArea.setText(content);

设置内容

下面是关于QQ登录界面,我们首先需要分三步,也就是以下三个包,还需要准备好登录需要的图片;
也就是images文件;
添加链接描述
提取码:lb8g
第一个包:

package 国玉;
import java.awt.*;
import javax.swing.*;
public class QQLogin {
	//1.为QQ登录界面整体初始化一个JFrame窗口
		private static JFrame jf=new JFrame();
	//QQ登录窗口初始化方法
		public static void initLogin() {
			BorderLayout border_Layout=new BorderLayout();
			JPanel panel_north=CreatePanel.CreateNorthPanel(jf);
			JPanel panel_center=CreatePanel.CrateCenterPanel(jf);
			JPanel panel_west=CreatePanel.CreateWestPanel();
			JPanel panel_south=CreatePanel.CreateSouthPanel();
			JPanel panel_east=CreatePanel.CreateEastPanel();
			jf.setSize(426, 300);                //设置窗口尺寸
			jf.setLocation(497, 242);            //设置窗口在屏幕显示位置、
			jf.setUndecorated(true);             //设置JFrame窗口边框不显示
			jf.setResizable(false);              //禁止改变窗口大小
	//2.根据QQ登陆界面效果,进行布局分布
			jf.setLayout(border_Layout);
			//2.1 创建并加入顶部面板
			jf.add(panel_north,BorderLayout.PAGE_START);
			//2.2 创建并加入中部面板
			jf.add(panel_center,BorderLayout.CENTER);
			//2.3 创建并加入左侧面板
			jf.add(panel_west,BorderLayout.LINE_START);
			//2.4 创建并加入底部面板
			jf.add(panel_south,BorderLayout.PAGE_END);
			//2.5 创建并加入右侧面板
			jf.add(panel_east,BorderLayout.LINE_END);
			jf.setVisible(true);                   //设置窗口可见
	      }
		public static void main(String[] args ) {
			SwingUtilities.invokeLater(QQLogin::initLogin);
		}
}

下面的这个是使用user.txt,创建一个文本文件user.txt记录用户名称和密码,修改QQ登录时判断是否登录成功的代码,使得在user.txt记录的用户才能够登录。

第二个包;

package 国玉;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*; 
	public class LoginListener implements ActionListener{
		private JComboBox <Object>jco;
		private JPasswordField jpa;
		private JFrame jf;
		public LoginListener(JComboBox<Object>jco,JPasswordField jpa,JFrame jf) {
			super();
			this.jco=jco;
			this.jpa=jpa;
			this.jf=jf;
		}
		boolean passwd(String name, String pwd) {
			String t = " " + name + " " + pwd + " ";
			BufferedReader br = null;
			try {
				br = new BufferedReader(new FileReader("D:\\user.txt"));
				String s = "";
				while ((s = br.readLine()) != null) {
					if (t.equals(s)) {
						return true;
					}
				}
				return false;
			} catch (Exception e2) {
				e2.printStackTrace();
			} finally {
				try {
					br.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return false;
		}

		public void actionPerformed(ActionEvent e) {
		    String name=(String)jco.getSelectedItem();
		    String pwd=new String(jpa.getPassword());
		    if(passwd(name, pwd)) {
		    	jf.dispose();
		    	JFrame jfn=new JFrame();
		    	jfn.setSize(289,687);
		    	//jfn.setDefaultCloseOperation(3);  
		    	jfn.setLocation(800, 100);
		    	jfn.setUndecorated(true);
		    	jfn.setResizable(true);
		    	jfn.setVisible(true);
		    	JPanel panel=new JPanel();
		    	panel.setLayout(null);
		    	panel.setPreferredSize(new Dimension(0,140));
		    	ImageIcon image=new ImageIcon("images/qqSuccess.jpg");
		    	JLabel background=new JLabel(image);
		    	background.setBounds(0, 0, 289, 687);
		    	panel.add(background);
		    	JButton out=new JButton(new ImageIcon("images/close2_normal.jpg"));
		    	out.setBounds(265, 0, 26, 26);
		    	out.setRolloverIcon(new ImageIcon("images/close2_hover_hover.jpg"));
		    	panel.add(out);
		    	jfn.add(panel);
		    	out.addActionListener(event ->jfn.dispose());
		    }else {
		    	JOptionPane.showMessageDialog(null, "您输入的账户或密码不正确,请重新输入");
		    }
		}
	}


第三个包

   package 国玉;
import java.awt.*;
//qq登录界面初始化方法
import javax.swing.*;
public class CreatePanel{
	private static LoginListener ll=null;
	public static JPanel CreateNorthPanel(JFrame jf){  
    	// 1、创建一个JPanel顶部面板
        JPanel panel=new JPanel();  
        // 取消面板内默认布局
        panel.setLayout(null);     
        // 设置顶部面板尺寸 
        panel.setPreferredSize(new Dimension(0, 140));  
        // 1.1、向顶部面板添加背景图片
        ImageIcon image=new ImageIcon("images/back.jpg");  
        JLabel background=new JLabel(image);  
        // 设置背景图片的位置及尺寸
        background.setBounds(0,0,426,image.getIconHeight());     
        panel.add(background);  
        // 1.2、在顶部JPanel面板右上角添加一个退出按钮
		JButton out = new JButton(new ImageIcon("images/close_normal.jpg"));
		out.addActionListener(event -> jf.dispose());
		out.setBounds(403,0,26,26);
		// 设置鼠标移动到退出按钮时更改图片
		out.setRolloverIcon(new ImageIcon("images/close_hover.jpg"));
		// 取消按钮边框效果
		out.setBorderPainted(false);
		panel.add(out);
		// 为退出按钮注册***,用来关闭窗口
		out.addActionListener(event -> jf.dispose());
        return panel;  
	}
//左侧面板区域
	    public static JPanel CreateWestPanel() {
			//左侧面板
			JPanel panel=new JPanel();
					panel.setLayout(null);
					panel.setPreferredSize(new Dimension(130,0));
					ImageIcon image=new ImageIcon("images/qq.jpg");
					JLabel background =new JLabel (image);
					background.setBounds(0,0,120,110);
					panel.add(background);
					return panel;
	    }
//中部面板区域
	    public static JPanel CrateCenterPanel(JFrame jf){
	    	//创建一个JPanel中部面板
	 	   JPanel panel=new JPanel();
	 	   panel.setLayout(null);
	 	   //3.1 创建一个JcomboBox下拉框组件,并初始化QQ账号
	 	  String str[]={"123456789","987654321","1314520888"};
	 	   //JComboBox jcoCenter=new JComboBox(str);
	 	  JComboBox<Object>jcoCenter=new JComboBox<Object>(str);
	 	   panel.add(jcoCenter);
	 	  //jcoCenter.setBorder(null);  
	 	   //设置下拉框可编辑
	 	   jcoCenter.setEditable(true);
	 	   jcoCenter.setBounds(0, 15, 175, 30);
	 	   //设置下拉框内容字体
	 	   jcoCenter.setFont(new Font("Calibri",0,13));
	 	   //3.2 创建一个JPasswordField密码框组件
	 	   JPasswordField jpaCenter=new JPasswordField();
	 	   //设置密码框面板为FlowLayout流式布局
	 	   jpaCenter.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
	 	   jpaCenter.setBounds(0,44,175,30);
	 	   jpaCenter.setPreferredSize(new Dimension(185,25));
	 	   panel.add(jpaCenter);
	 	   //3.3 创建ImageIcon小键盘组件,并加入到密码框组件中
	 	   ImageIcon image=new ImageIcon("images/keyboard.jpg");
	 	   JButton jbu=new JButton(image);
	 	   jbu.setPreferredSize(new Dimension(22,20));
	 	   jbu.setBorderPainted(false);
	 	   jpaCenter.add(jbu);
	 	   //3.4 创建两个JCheckBox多选框组件
	 	   JCheckBox jch1=new JCheckBox("记住密码");
	 	   //设置选中时不显示边框
	 	   jch1.setFocusPainted(false);
	 	   jch1.setFont(new Font("宋体",0,13));
	 	   jch1.setBounds(0,85,80,20);
	 	   panel.add(jch1);
	 	   JCheckBox jch2=new JCheckBox("自动登录");
	 	   jch2.setFocusPainted(false);
	 	   jch2.setFont(new Font("宋体",0,12));
	 	   jch2.setBounds(100,85,80,20);
	 	   panel.add(jch2);
	 	   ll=new LoginListener(jcoCenter,jpaCenter,jf);
	 	   return panel;
	    }
//右侧面板区域
	    public static JPanel CreateEastPanel(){
	    	//创建一个Jpanel右侧面板
	    	JPanel panel=new JPanel();
	    	panel.setLayout(null);
	    	panel.setPreferredSize(new Dimension(100,0));
	    	//创建两个JLabel标签组件
	    	JLabel regeist=new JLabel("注册账号");
	    	regeist.setForeground(new Color(100,149,238));
	    	regeist.setBounds(0,13,60,30);
	    	regeist.setFont(new Font("宋体",0,12));
	    	//创建一个JPanel右侧面板
	    	JLabel regetpwd =new JLabel("找回密码");
	    	regetpwd.setForeground(new Color(100,149,238));
	    	regetpwd.setBounds(0,43,60,30);
	    	regetpwd.setFont(new Font("宋体",0,12));
	    	panel.add(regetpwd);
	    	panel.add(regeist);
	    	return panel;
	    	}

//底部面版区域
	public static JPanel CreateSouthPanel() {
		//6.创建一个JPanel底部面板
		JPanel panel=new JPanel();
		panel.setPreferredSize(new Dimension(0,51));
		panel.setLayout(null);
		//6.1  创建左下角多人登录图标组件
		JButton jble=new JButton(new ImageIcon("images/single_normal.jpg"));
		jble.setPreferredSize(new Dimension(40,40));//
		jble.setFocusPainted(false);
		jble.setRolloverIcon(new ImageIcon("images/single_down.jpg"));
		jble.setBorderPainted(false);
		//设置不显示按钮区域
		jble.setContentAreaFilled(false);
		jble.setBounds(0,10,40,40);
		jble.setToolTipText("多账号登录");
		//6.2 创建底部中间登陆图标组件
		ImageIcon image=new ImageIcon("images/login_normal.jpg");
		JButton jb=new JButton("登         录",image);
		jb.addActionListener(ll);
		jb.setFont(new Font("宋体",0,13));//Font宋体 
		jb.setBounds(130,0,175,40);
		//将文字放在图片中间
		jb.setHorizontalTextPosition(SwingConstants.CENTER);
		jb.setFocusPainted(false);
		jb.setContentAreaFilled(false);
		jb.setBorderPainted(false);
		jb.setRolloverIcon(new ImageIcon("images/login_hover.jpg"));
		//6.3 创建右下角二维码登录图标组件
		JButton jbri=new JButton (new ImageIcon("images/right_normal.jpg"));
		jbri.setBounds(380,10,40,40);
		jbri.setFocusPainted(false);
		jbri.setBorderPainted(false);
		jbri.setContentAreaFilled(false);
		jbri.setRolloverIcon(new ImageIcon("images/right_hover.jpg"));
		jbri.setToolTipText("二维码登录");
		//将底部3个组件添加到底部JPanel面板中
		panel.add(jble);
		panel.add(jb);
		panel.add(jbri);
		return panel;
	}
}

第四个包与第二个实现结果一样;

package 国玉;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
	public class LoginListener implements ActionListener{
		private JComboBox <Object>jco;
		private JPasswordField jpa;
		private JFrame jf;
		public LoginListener(JComboBox<Object>jco,JPasswordField jpa,JFrame jf) {
			super();
			this.jco=jco;
			this.jpa=jpa;
			this.jf=jf;
		}
		public void actionPerformed(ActionEvent e) {
		    String name=(String)jco.getSelectedItem();
		    String pwd=new String(jpa.getPassword());
		    if(name.equals("123456789")&&pwd.equals("123")) {
		    	jf.dispose();
		    	JFrame jfn=new JFrame();
		    	jfn.setSize(289,687);
		    	//jfn.setDefaultCloseOperation(3);  
		    	jfn.setLocation(800, 100);
		    	jfn.setUndecorated(true);
		    	jfn.setResizable(true);
		    	jfn.setVisible(true);
		    	JPanel panel=new JPanel();
		    	panel.setLayout(null);
		    	panel.setPreferredSize(new Dimension(0,140));
		    	ImageIcon image=new ImageIcon("images/qqSuccess.jpg");
		    	JLabel background=new JLabel(image);
		    	background.setBounds(0, 0, 289, 687);
		    	panel.add(background);
		    	JButton out=new JButton(new ImageIcon("images/close2_normal.jpg"));
		    	out.setBounds(265, 0, 26, 26);
		    	out.setRolloverIcon(new ImageIcon("images/close2_hover_hover.jpg"));
		    	panel.add(out);
		    	jfn.add(panel);
		    	out.addActionListener(event ->jfn.dispose());
		    }else {
		    	JOptionPane.showMessageDialog(null, "您输入的账户或密码不正确,请重新输入");
		    }
		}
	}

当输入密码成功时;

输入密码失败时;

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务