JAVA中GUI登录界面设计源代码是什么?

JAVA中GUI登录界面设计源代码是什么?,第1张

importjava.awt.BorderLayout\x0d\x0aimportjava.awt.Container\x0d\x0aimportjava.awt.FlowLayout\x0d\x0aimportjavax.swing.JButton\x0d\x0aimportjavax.swing.JFrame\x0d\x0aimportjavax.swing.JLabel\x0d\x0aimportjavax.swing.JPanel\x0d\x0aimportjavax.swing.JPasswordField\x0d\x0aimportjavax.swing.JTextField\x0d\x0a\x0d\x0apublicclassLogin{\x0d\x0a\x0d\x0aprivateJFrameframe=newJFrame("登春樱并扒迹录")\x0d\x0aprivateContainerc=frame.getContentPane()\x0d\x0aprivateJTextFieldusername=newJTextField()\x0d\x0aprivateJPasswordFieldpassword=newJPasswordField()\x0d\x0aprivateJButtonok=newJButton("确定")\x0d\x0aprivateJButtoncancel=newJButton("取消")\x0d\x0apublicLogin(){\x0d\x0aframe.setSize(300,200)\x0d\x0ac.setLayout(newBorderLayout())\x0d\x0ainitFrame()\x0d\x0aframe.setVisible(true)\x0d\x0a}\x0d\x0a\x0d\x0aprivatevoidinitFrame(){\x0d\x0a\x0d\x0a//顶部\x0d\x0aJPaneltitlePanel=newJPanel()\x0d\x0atitlePanel.setLayout(newFlowLayout())\x0d\x0atitlePanel.add(newJLabel("系统管颂滑理员登录"))\x0d\x0ac.add(titlePanel,"North")\x0d\x0a\x0d\x0a//中部表单\x0d\x0aJPanelfieldPanel=newJPanel()\x0d\x0afieldPanel.setLayout(null)\x0d\x0aJLabell1=newJLabel("用户名:")\x0d\x0al1.setBounds(50,20,50,20)\x0d\x0aJLabell2=newJLabel("密码:")\x0d\x0al2.setBounds(50,60,50,20)\x0d\x0afieldPanel.add(l1)\x0d\x0afieldPanel.add(l2)\x0d\x0ausername.setBounds(110,20,120,20)\x0d\x0apassword.setBounds(110,60,120,20)\x0d\x0afieldPanel.add(username)\x0d\x0afieldPanel.add(password)\x0d\x0ac.add(fieldPanel,"Center")\x0d\x0a\x0d\x0a//底部按钮\x0d\x0aJPanelbuttonPanel=newJPanel()\x0d\x0abuttonPanel.setLayout(newFlowLayout())\x0d\x0abuttonPanel.add(ok)\x0d\x0abuttonPanel.add(cancel)\x0d\x0ac.add(buttonPanel,"South")\x0d\x0a}\x0d\x0a\x0d\x0apublicstaticvoidmain(String[]args){\x0d\x0anewLogin()\x0d\x0a}\x0d\x0a\x0d\x0a}

界面代码写的不错 其实连数据库也不复杂。 如果希望不转换界面,登陆和登陆成功或失败后的信息在同意界面 可以设置个int类型的变量 来控制显示信息

下面是按你的要求写的 现实界面的转换 如果用户名和密码正确进入MyFrame1窗口 如果不正确进入MyFrame2窗口。

连数据库的时候先添加数据库驱动包,如果连接数据库有问题 多打印一饥模判下相关数据 很容易发现错误之处的

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.sql.Connection

import java.sql.DriverManager

import java.sql.ResultSet

import java.sql.Statement

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPasswordField

import javax.swing.JTextField

import java.awt.Color

import java.awt.Frame

import java.awt.Graphics

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

public class 登陆界面 extends JFrame {

private static final long serialVersionUID = 1L

private JPasswordField passwordField

private JTextField textField

public static void main(String args[]) {

try {

登陆界面 frame = new 登陆界面()

frame.setVisible(true)

} catch (Exception e) {

e.printStackTrace()

}

}

/**

* Create the frame

*/

public 登陆界面() {

super()

getContentPane().setLayout(null)

setBounds(100, 100, 500, 375)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

final JButton button = new JButton()

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String dir = "com.mysql.jdbc.Driver"//数据库驱动类,我这是MySQL的 ,连数据库记得导入驱动的jar包

Connection conn = null

Statement stat = null

ResultSet rs = null

try{

Class.forName(dir)

String url = "jdbc:mysql://localhost:3306/hospital?user=root&password=root"//数据库名 hospital用户名和密码烂改都是root

conn = DriverManager.getConnection(url)

stat = conn.createStatement()

//得到用户名

String name = textField.getText().trim()

//得到密码

char[] pass = passwordField .getPassword()

String password = new String(pass)

//输出用户名密码

System.out.println(name)

System.out.println(password)

//数据库表user,字段用户名为码滚username,密码password,都是varchar型

String sql = "select username,password from user where username = '" + name + "'"

rs = stat.executeQuery(sql.toString())

if(rs.next()){

//获得数据库里面的用户名和密码

String sname = rs.getString("username")

String spass = rs.getString("password")

//对输入的用户名和密码和数据库里的进行判断

if((rs != null ) &&(name.equals(sname)) &&(password.equals(spass))) {

//登陆成功释放登陆界面资源,显示a窗口

登陆界面.this.dispose()

new MyFrame1().launch()

System.out.println("chenggong")

} else {

//密码错误,显示b窗口

登陆界面.this.dispose()

new MyFrame2().launch()

System.out.println("mimacuowu")

}

} else {

//用户名错误和其他情况,显示b窗口

登陆界面.this.dispose()

new MyFrame2().launch()

System.out.println("yonghumingcuowu")

}

}catch(Exception e1){

//数据库连接失败,显示b窗口

登陆界面.this.dispose()

new MyFrame2().launch()

System.out.println("数据库连接错误")

}finally{

try{

if(rs!=null) rs.close()

if(stat!=null) stat.close()

if(conn!=null) conn.close()

}catch(Exception e1){

}

}

}

})

button.setText("确定")

button.setBounds(62, 273, 101, 25)

getContentPane().add(button)

final JButton button_1 = new JButton()

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {System.exit(0)

}

})

button_1.setText("取消")

button_1.setBounds(325, 273, 101, 25)

getContentPane().add(button_1)

final JLabel label = new JLabel()

label.setText("用户名")

label.setBounds(23, 60, 60, 15)

getContentPane().add(label)

final JLabel label_1 = new JLabel()

label_1.setText("密码")

label_1.setBounds(23, 172, 60, 15)

getContentPane().add(label_1)

textField = new JTextField()

textField.setBounds(48, 115, 90, 21)

getContentPane().add(textField)

passwordField = new JPasswordField()

passwordField.setBounds(42, 216, 96, 25)

getContentPane().add(passwordField)

}

}

class MyFrame1 extends Frame {

int orgX1

int orgY1

int orgX2

int orgY2

public void launch() {

setBounds(500,500,500,500)

setVisible(true)

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

e.getWindow().dispose()

System.exit(0)

}

})

}

public void paint(Graphics g) {

//Color c =g.getColor()

g.setColor(Color.red)

g.drawLine(100, 50, 100, 80)

}

}

class MyFrame2 extends Frame {

int orgX1

int orgY1

int orgX2

int orgY2

public void launch() {

setBounds(500,500,500,500)

setVisible(true)

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

e.getWindow().dispose()

System.exit(0)

}

})

}

public void paint(Graphics g) {

//Color c =g.getColor()

g.setColor(Color.red)

g.draw3DRect(100, 100, 100, 100,true)

}

}

meo a

emie

ae

emi oe la

aenia em a

eja sd e

好了给分吧渗嫌空丛瞎者饥


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12306670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存