java swing 创建一个简单的QQ界面教程

记录自己用java swing做的第一个简易界面。

LoginAction.java

package com.QQUI0819;

import javax.swing.*;
import java.awt.event.*;
//首先,编写按钮癿监听器实现类
public class LoginAction implements
  ActionListener {
 private int count=0;
 //当前为null,创建后指向界面输入框
 private JTextField ulName;
 private JTextField ulpass;
 //创建时,输入界面类中癿输入框
 public LoginAction(JTextField ulName,JTextField ulpass ){
  this.ulName=ulName;
  this.ulpass=ulpass;
 }

 //实现接口中癿方法
 //当劢作发生时,这个方法执行
 public void actionPerformed(ActionEvent e) {
  //点击时,就取得界面输入框癿内容
  //此时癿jtf,指向是界面上那个输入框
  String u=ulName.getText();
  String p=ulName.getText();
  System.out.println("账号输入的是 "+u);
  System.out.println("密码输入的是 "+p)
  if(u.equals("csy123") ||(p.equals("456"))){
   //如果输入正确,弹出新界面
   JFrame jf=new JFrame();
   jf.setTitle("登陆成功");
   jf.setSize(300,400);
   jf.setLocationRelativeTo(null);
   jf.setVisible(true);
  } else {
   //如果输入正确,弹出新界面
   JFrame jf=new JFrame();
   jf.setTitle("登陆失败");
   jf.setSize(300,100);
   JButton b1 = new JButton("登陆失败,账号和密码不匹配");
   jf.add(b1);
   jf.setLocationRelativeTo(null);
   jf.setVisible(true);
  }
 }
}

QQ.java

package com.QQUI0819;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;

public class QQ extends JFrame{

 //用户名
 private JTextField ulName;
 //密码
 private JPasswordField ulPasswd;
 //小容器
 private JLabel j1;
 private JLabel j2;
 private JLabel j3;
 private JLabel j4;
 //小按钮
 private JButton b1;
 //复选框
 private JCheckBox c1;
 private JCheckBox c2;
 /**
 * 初始化QQ登录页面
 * */
 public QQ(){
 //设置登录窗口标题
 this.setTitle("QQ登录");
 //去掉窗口的装饰(边框)
 //采用指定的窗口装饰风格
 this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
 //窗体组件初始化
 init();
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //设置布局为绝对定位
 this.setLayout(null);
 this.setBounds(0, 0, 355, 265);
 //设置窗体的图标
 Image img0 = new ImageIcon("E:\\蓝杰培训\\第一周java基础\\basicStudy\\src\\com\\QQUI0819\\QQlogo.jpg").getImage();
 this.setIconImage(img0);
 //窗体大小不能改变
 this.setResizable(false);
 //居中显示
 this.setLocationRelativeTo(null);
 //窗体显示
 this.setVisible(true);
 }
 /**
 * 窗体组件初始化
 * */
 public void init(){
 //创建一个容器,其中的图片大小和setBounds
 Container container = this.getContentPane();
 j1 = new JLabel();
 //设置背景色
 Image img1 = new ImageIcon("E:\\蓝杰培训\\第一周java基础\\basicStudy\\src\\com\\QQUI0819\\QQ.jpg").getImage();
 j1.setIcon(new ImageIcon(img1));
 j1.setBounds(0, 0, 355, 90);
 //qq头像设定
 j2 = new JLabel();
 Image img2 = new ImageIcon("E:\\蓝杰培训\\第一周java基础\\basicStudy\\src\\com\\QQUI0819\\QQlogo.jpg").getImage();
 j2.setIcon(new ImageIcon(img2));
 j2.setBounds(20, 100, 60, 63);
 //用户名输入框
 ulName = new JTextField();
 ulName.setBounds(100, 100, 150, 20);
 //注册账号
 j3 = new JLabel("注册账号");
 j3.setBounds(260, 100, 70, 20);
 //密码输入框
 ulPasswd = new JPasswordField();
 ulPasswd.setBounds(100, 130, 150, 20);
 //找回密码
 j4= new JLabel("找回密码");
 j4.setBounds(260, 130, 70, 20);
 //记住密码
 c1 = new JCheckBox("记住密码");
 c1.setBounds(105, 155, 80, 15);
 //自动登陆
 c2 = new JCheckBox("自动登陆");
 c2.setBounds(185, 155, 80, 15);
 //登陆按钮
 b1 = new JButton("登录");
 //设置字体和颜色和手形指针
 b1.setFont(new Font("宋体", Font.PLAIN, 12));
 b1.setForeground(Color.black);
 b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 b1.setBounds(20, 200, 300, 20);
 //给按钮添加
 //所有组件用容器装载
 this.add(j1);
 this.add(j2);
 this.add(j3);
 this.add(j4);
 this.add(c1);
 this.add(c2);
 this.add(b1);
 //创建监听器对象,幵加给按钮
 LoginAction lo=new LoginAction(ulName,ulPasswd);
 b1.addActionListener(lo);

 container.add(j1);
 container.add(ulName);
 container.add(ulPasswd);
 }
 public static void main(String[] args) {
 new QQ();
 }
}

效果图

素材

logo:

QQ logo:

补充知识:创建一个简单的Java Swing登入窗口

连接数据图就不弄了,要连接数据库的自行添加

为了方便排版,Layout用SpringLayout类型

1.账号框采用JTextField 类型

2.密码框采用JPasswordField类型

两者的区别是:

JPasswordField显示的文本是这样的:

文本框JTextField显示的文本是这样的:

3.建立两个JButton,一个是“登入”按钮,另一个是“重置”按钮

4.建立两个Checkbox:一个是记住密码,另一个是隐身登入

5.最后一步加入背景图

代码:

package l;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JLayeredPane;
import java.awt.Checkbox;

public class Login extends JFrame {

  private JPanel contentPane;
  private JTextField textField;
  private JPasswordField passwordField;  
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Login frame = new Login();
          frame.setVisible(true);              //窗口可见
          frame.setResizable(false);           //设置登入窗口不能调整大小
          ImageIcon img = new ImageIcon("d:/img.jpg");//这是背景图片
           JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
           frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));     
           imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());
            Container cp=frame.getContentPane();
            ((JPanel)cp).setOpaque(false);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
  
  public Login() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    SpringLayout sl_contentPane = new SpringLayout();
    contentPane.setLayout(sl_contentPane);
    
    JLabel lblNewLabel = new JLabel("账号:");
    lblNewLabel.setFont(new Font("黑体", Font.PLAIN, 19));
    sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 34, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, 41, SpringLayout.WEST, contentPane);
    contentPane.add(lblNewLabel);
    
    JLabel lblNewLabel_1 = new JLabel("密码:");
    lblNewLabel_1.setFont(new Font("黑体", Font.PLAIN, 19));
    sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 29, SpringLayout.SOUTH, lblNewLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_1, 0, SpringLayout.WEST, lblNewLabel);
    contentPane.add(lblNewLabel_1);
    
    textField = new JTextField();
    textField.addFocusListener(new FocusAdapter() {
      @Override
      public void focusLost(FocusEvent e) {
        String temp=textField.getText();
        if(temp.equals("")) {
          textField.setForeground(Color.GRAY);               //JTextField提示输入QQ号
          textField.setText("请输入QQ号");
        }
      }
      
      public void focusGained(FocusEvent e) {
        String temp=textField.getText();
        if(temp.equals("请输入QQ号")) {
          textField.setText("");
        }
      }
    });
    sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 1, SpringLayout.NORTH, lblNewLabel);
    sl_contentPane.putConstraint(SpringLayout.WEST, textField, 52, SpringLayout.EAST, lblNewLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST, textField, 196, SpringLayout.EAST, lblNewLabel);
    contentPane.add(textField);
    textField.setColumns(10);
    
    passwordField = new JPasswordField();
    sl_contentPane.putConstraint(SpringLayout.NORTH, passwordField, 26, SpringLayout.SOUTH, textField);
    sl_contentPane.putConstraint(SpringLayout.WEST, passwordField, 52, SpringLayout.EAST, lblNewLabel_1);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, passwordField, 48, SpringLayout.SOUTH, textField);
    sl_contentPane.putConstraint(SpringLayout.EAST, passwordField, 201, SpringLayout.EAST, lblNewLabel_1);
    contentPane.add(passwordField);
    
    JButton btnNewButton = new JButton("登入");
    sl_contentPane.putConstraint(SpringLayout.SOUTH, btnNewButton, 65, SpringLayout.SOUTH, passwordField);
    contentPane.add(btnNewButton);
    
    JButton button = new JButton("重置");
    sl_contentPane.putConstraint(SpringLayout.NORTH, btnNewButton, 2, SpringLayout.NORTH, button);
    sl_contentPane.putConstraint(SpringLayout.EAST, btnNewButton, -70, SpringLayout.WEST, button);
    sl_contentPane.putConstraint(SpringLayout.NORTH, button, 32, SpringLayout.SOUTH, passwordField);
    sl_contentPane.putConstraint(SpringLayout.WEST, button, 242, SpringLayout.WEST, contentPane);
    button.setFont(new Font("宋体", Font.PLAIN, 19));
    contentPane.add(button);
    
    Checkbox checkbox = new Checkbox("记住密码");
    sl_contentPane.putConstraint(SpringLayout.WEST, checkbox, 0, SpringLayout.WEST, textField);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, checkbox, -6, SpringLayout.NORTH, btnNewButton);
    contentPane.add(checkbox);
    
    Checkbox checkbox_1 = new Checkbox("隐身登入");
    sl_contentPane.putConstraint(SpringLayout.WEST, checkbox_1, 35, SpringLayout.EAST, checkbox);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, checkbox_1, 0, SpringLayout.SOUTH, checkbox);
    contentPane.add(checkbox_1);
  }
}

运行截图:

以上这篇java swing 创建一个简单的QQ界面教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持菜鸟教程(cainiaojc.com)。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。