用java怎么实现QQ登录界面?

Python019

用java怎么实现QQ登录界面?,第1张

用java做QQ登录界面的写法如下:

package ch10

import java.awt.*

import java.awt.event.*

import javax.swing.*

1、//定义该类继承自JFrame,实现ActionListener接口

public class LoginTest extends JFrame implements ActionListener

{

2、//创建JPanel对象

private JPanel jp=new JPanel()

3、//创建3个标并加入数组

JLabel name = new JLabel("请输入用户名")

JLabel password = new JLabel("请输入密码")

JLabel show = new JLabel("")

private JLabel[] jl={name,password,show}

4、//创建登陆和重置按扭并加入数组

JButton login = new JButton("登陆")

JButton reset = new JButton("重置")

private JButton[] jb={login,reset}

5、//创建文本框以及密码框

private JTextField jName=new JTextField()

private JPasswordField jPassword =new JPasswordField()

public LoginTest()

{

6、//设置布局管理器为空布局,这里自己摆放按钮、标签和文本框

jp.setLayout(null)

for(int i=0i<2i++)

{

7、//设置标签和按扭的位置与大小

jl[i].setBounds(30,20+40*i,180,20)

jb[i].setBounds(30+110*i,100,80,20)

8、//添加标签和按扭到JPanel容器中

jp.add(jl[i])

jp.add(jb[i])

//为2个按钮注册动作事件监听器

jb[i].addActionListener(this)

}

9、//设置文本框的位置和大小,注意满足美观并足够用户名的长度

jName.setBounds(130,15,100,20)

10、//添加文本框到JPanel容器中

jp.add(jName)

11、//为文本框注册动作事件监听器

jName.addActionListener(this)

12、//设置密码框的位置和大小,注意满足美观和足够密码的长度

jPassword.setBounds(130,60,100,20)

13、//添加密码框到JPanel容器中

jp.add(jPassword)

14、//设置密码框中的回显字符,这里设置美元符号

jPassword.setEchoChar('$')

15、//为密码框注册动作事件监听器

jPassword.addActionListener(this)

16、//设置用于显示登陆状态的标签大小位置,并将其添加进JPanel容器

jl[2].setBounds(10,180,270,20)

jp.add(jl[2])

17、//添加JPanel容器到窗体中

this.add(jp)

18、//设置窗体的标题、位置、大小、可见性及关闭动作

this.setTitle("登陆窗口")

this.setBounds(200,200,270,250)

this.setVisible(true)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

19、//实现动作监听器接口中的方法actionPerformed

public void actionPerformed(ActionEvent e)

{

20、//如果事件源为文本框

if(e.getSource()==jName)

{

21、//切换输入焦点到密码框

jPassword.requestFocus()

}

22、//如果事件源为重置按扭

else if(e.getSource()==jb[1])

{

23、//清空姓名文本框、密码框和show标签中的所有信息

jl[2].setText("")

jName.setText("")

jPassword.setText("")

24、//让输入焦点回到文本框

jName.requestFocus()

}

25、//如果事件源为登陆按钮,则判断登录名和密码是否正确

else

{

26、//判断用户名和密码是否匹配

if(jName.getText().equals("lixiangguo")&&

String.valueOf(jPassword.getPassword()).equals("19801001"))

{

27、jl[2].setText("登陆成功,欢迎您的到来!")

}

else

{

28、jl[2].setText("对不起,您的用户名或密码错误!")

}

}

}

public static void main(String[] args)

{

29、//创建LoginTest窗体对象

new LoginTest()

}

}

import java.awt.*

import javax.swing.*

import java.awt.event.*

import java.sql.*

class LoginFrm extends JFrame implements ActionListener

{

JLabel lbl1=new JLabel("用户名")

JLabel lbl2=new JLabel("密码")

JTextField txt=new JTextField(15)

JPasswordField pf=new JPasswordField()

JButton btn1=new JButton("确定")

JButton btn2=new JButton("取消")

public LoginFrm()

{

this.setTitle("登陆")

JPanel jp=(JPanel)this.getContentPane()

jp.setLayout(new GridLayout(3,2,10,10))

jp.add(lbl1)jp.add(txt)

jp.add(lbl2)jp.add(pf)

jp.add(btn1)jp.add(btn2)

btn1.addActionListener(this)

btn2.addActionListener(this)

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==btn1)

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","")

Statement cmd=con.createStatement()

ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'")

if(rs.next())

{

JOptionPane.showMessageDialog(null,"登陆成功!")

}

else

JOptionPane.showMessageDialog(null,"用户名或密码错误!")

} catch(Exception ex){}

if(ae.getSource()==btn2)

{

txt.setText("")

pf.setText("")

}

}

}

public static void main(String arg[])

{

JFrame.setDefaultLookAndFeelDecorated(true)

LoginFrm frm=new LoginFrm()

frm.setSize(400,200)

frm.setVisible(true)

}

}

你要先学会截图哦,你发的看不清楚,重新写了一个你参考参考!

import java.awt.GridLayout

import javax.swing.ButtonGroup

import javax.swing.JButton

import javax.swing.JComboBox

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

import javax.swing.JRadioButton

import javax.swing.JTextField

public class Day30A extends JFrame {

private static final long serialVersionUID = 1L

private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar

private JComboBox<String>jcb

private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7

private ButtonGroup btg

private JRadioButton jr1,jr2

Day30A(){

this.setTitle("注册账户")

this.setLayout(new GridLayout(7,1))

this.setSize(300,280)

this.setLocationRelativeTo(null)

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

init()

this.setVisible(true)

}

private void init() {

String str="卡片类型1,卡片类型2,卡片类型3,卡片类型4,卡片类型5"

jcb=new JComboBox<>(str.split(","))

labelId=new JLabel("账号: ")

labelName=new JLabel("姓名: ")

labelPass=new JLabel("密码: ")

labelMoney=new JLabel("开户金额:")

labelSelect=new JLabel("存款类型:")

labelCar=new JLabel("卡片类型:")

addFun1()

addFun2()

}

private void addFun2() {

this.add(jp1)

this.add(jp2)

this.add(jp3)

this.add(jp4)

this.add(jp5)

this.add(jp6)

this.add(jp7)

}

private void addFun1() {

jp1=new JPanel()

jp1.add(labelId)

jp1.add(new JTextField(15))

jp2=new JPanel()

jp2.add(labelName)

jp2.add(new JTextField(15))

jp3=new JPanel()

jp3.add(labelPass)

jp3.add(new JTextField(15))

jp4=new JPanel()

jp4.add(labelMoney)

jp4.add(new JTextField(13))

jp5=new JPanel()

jp5.add(labelSelect)

btg=new ButtonGroup()

jr1=new JRadioButton("定期")

jr2=new JRadioButton("活期",true)

btg.add(jr1)

btg.add(jr2)

jp5.add(jr1)

jp5.add(jr2)

jp6=new JPanel()

jp6.add(labelCar)

jp6.add(jcb)

jp7=new JPanel()

jp7.add(new JButton("确定"))

jp7.add(new JButton("取消"))

}

public static void main(String[] args) {

new Day30A()

}

}