java swing 创建一个简单的QQ界面教程
作者:陈思源一点也不圆 时间:2022-09-08 06:51:39
记录自己用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);
}
}
运行截图:
来源:https://blog.csdn.net/weixin_49857492/article/details/108513573
标签:java,swing,QQ界面
0
投稿
猜你喜欢
Android自定义控件之组合控件学习笔记分享
2022-09-18 01:09:22
java实现简单的webservice方式
2023-11-25 03:59:58
Android使用SoundPool播放短音效
2022-03-02 12:33:00
Android LayerDrawable超详细讲解
2023-12-03 16:36:30
mybatis中使用oracle关键字出错的解决方法
2022-05-14 10:19:23
Ubuntu中为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
2022-07-20 15:09:51
C# 写入XML文档三种方法详细介绍
2021-11-08 23:20:08
详解MybatisPlus中@Version注解的使用
2023-11-09 23:49:17
C#中DataTable 转换为 Json的方法汇总(三种方法)
2021-12-12 16:41:00
Java设计通用的返回数据格式过程讲解
2023-11-09 00:16:40
Android自定义Seekbar滑动条 Pop提示跟随滑动按钮滑动
2021-11-02 20:32:06
关于Flyweight模式应用实践的相关介绍
2021-07-28 21:50:34
SpringBoot使用@ResponseBody返回图片的实现
2023-11-28 04:41:24
java selenium Selenium IDE介绍及用法
2023-11-21 06:41:22
混合语言编程—C#使用原生的Directx和OpenGL绘图的方法
2022-06-18 14:36:41
详解C#对路径...的访问被拒绝解决过程
2021-06-01 11:12:10
JAVA读取属性文件的几种方法总结
2023-10-21 04:36:40
hadoop运行java程序(jar包)并运行时动态指定参数
2023-07-27 11:02:10
Android实现列表数据按名称排序、中英文混合排序
2022-10-18 15:15:57
将文件夹下所有文件输出到日志文件中 c#递归算法学习示例
2023-04-08 20:36:22