资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java简单登录代码,java简单代码运行

java实现简单登录界面

自己写的比较规范的代码,都有注释:

创新互联是专业的库尔勒网站建设公司,库尔勒接单;提供成都网站制作、网站设计、外贸网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行库尔勒网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

import javax.swing.JFrame;//框架

import javax.swing.JPanel;//面板

import javax.swing.JButton;//按钮

import javax.swing.JLabel;//标签

import javax.swing.JTextField;//文本框

import java.awt.Font;//字体

import java.awt.Color;//颜色

import javax.swing.JPasswordField;//密码框

import java.awt.event.ActionListener;//事件监听

import java.awt.event.ActionEvent;//事件处理

import javax.swing.JOptionPane;//消息窗口

public class UserLogIn extends JFrame{

public JPanel pnluser;

public JLabel lbluserLogIn;

public JLabel lbluserName;

public JLabel lbluserPWD;

public JTextField txtName;

public JPasswordField pwdPwd;

public JButton btnSub;

public JButton btnReset;

public UserLogIn(){

pnluser = new JPanel();

lbluserLogIn = new JLabel();

lbluserName = new JLabel();

lbluserPWD = new JLabel();

txtName = new JTextField();

pwdPwd = new JPasswordField();

btnSub = new JButton();

btnReset = new JButton();

userInit();

}

public void userInit(){

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序

this.setSize(300,200);//设置框架大小为长300,宽200

this.setResizable(false);//设置框架不可以改变大小

this.setTitle("用户登录");//设置框架标题

this.pnluser.setLayout(null);//设置面板布局管理

this.pnluser.setBackground(Color.cyan);//设置面板背景颜色

this.lbluserLogIn.setText("用户登录");//设置标签标题

this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体

this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色

this.lbluserName.setText("用户名:");

this.lbluserPWD.setText("密 码:");

this.btnSub.setText("登录");

this.btnReset.setText("重置");

this.lbluserLogIn.setBounds(120,15,60,20);//设置标签x坐标120,y坐标15,长60,宽20

this.lbluserName.setBounds(50,55,60,20);

this.lbluserPWD.setBounds(50,85,60,25);

this.txtName.setBounds(110,55,120,20);

this.pwdPwd.setBounds(110,85,120,20);

this.btnSub.setBounds(85,120,60,20);

this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口

{

public void actionPerformed(ActionEvent e){

btnsub_ActionEvent(e);

}

}

);

this.btnReset.setBounds(155,120,60,20);

this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口

{

public void actionPerformed(ActionEvent e){

btnreset_ActionEvent(e);

}

}

);

this.pnluser.add(lbluserLogIn);//加载标签到面板

this.pnluser.add(lbluserName);

this.pnluser.add(lbluserPWD);

this.pnluser.add(txtName);

this.pnluser.add(pwdPwd);

this.pnluser.add(btnSub);

this.pnluser.add(btnReset);

this.add(pnluser);//加载面板到框架

this.setVisible(true);//设置框架可显

}

public void btnsub_ActionEvent(ActionEvent e){

String name = txtName.getText();

String pwd = String.valueOf(pwdPwd.getPassword());

if(name.equals("")){

JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);

return;

}else if (pwd.equals("")){

JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);

return;

}else if(true){

this.dispose();

}else{

JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);

return;

}

}

public void btnreset_ActionEvent(ActionEvent e){

txtName.setText("");

pwdPwd.setText("");

}

public static void main(String[] args){

new UserLogIn();

}

}

登陆界面的java代码怎么写?

概述

具体框架使用jframe,文本框组件:JTextField;密码框组件:JPasswordField;标签组件:JLabel;复选框组件:JCheckBox;单选框组件:JRadioButton;按钮组件JButton。

登录界面:

代码实例

import javax.swing.*;

import java.awt.*;   //导入必要的包

public class denglu extends JFrame{

JTextField jTextField ;//定义文本框组件

JPasswordField jPasswordField;//定义密码框组件

JLabel jLabel1,jLabel2;

JPanel jp1,jp2,jp3;

JButton jb1,jb2; //创建按钮

public denglu(){

jTextField = new JTextField(12);

jPasswordField = new JPasswordField(13);

jLabel1 = new JLabel("用户名");

jLabel2 = new JLabel("密码");

jb1 = new JButton("确认");

jb2 = new JButton("取消");

jp1 = new JPanel();

jp2 = new JPanel();

jp3 = new JPanel();

//设置布局

this.setLayout(new GridLayout(3,1));

jp1.add(jLabel1);

jp1.add(jTextField);//第一块面板添加用户名和文本框

jp2.add(jLabel2);

jp2.add(jPasswordField);//第二块面板添加密码和密码输入框

jp3.add(jb1);

jp3.add(jb2); //第三块面板添加确认和取消

//        jp3.setLayout(new FlowLayout());  //因为JPanel默认布局方式为FlowLayout,所以可以注销这段代码.

this.add(jp1);

this.add(jp2);

this.add(jp3);  //将三块面板添加到登陆框上面

//设置显示

this.setSize(300, 200);

//this.pack();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

this.setTitle("登陆");

}

public static void main(String[] args){

new denglu();

}

}

拓展内容

java swing包

Swing 是一个为Java设计的GUI工具包。

Swing是JAVA基础类的一部分。

Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

概念解析:

JFrame – java的GUI程序的基本思路是以JFrame为基础,它是屏幕上window的对象,能够最大化、最小化、关闭。

JPanel – Java图形用户界面(GUI)工具包swing中的面板容器类,包含在javax.swing 包中,可以进行嵌套,功能是对窗体中具有相同逻辑功能的组件进行组合,是一种轻量级容器,可以加入到JFrame窗体中。。

JLabel – JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对齐;而只显示图像的标签则水平居中对齐。

JTextField –一个轻量级组件,它允许编辑单行文本。

JPasswordField – 允许我们输入了一行字像输入框,但隐藏星号(*) 或点创建密码(密码)

JButton – JButton 类的实例。用于创建按钮类似实例中的 "Login"。

用java写一个登录界面的代码,哪位大神会啊,谢谢。

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class Test26 {

public static void main(String[] args) {

final String userName = "abc";

final String passwrod = "123";

JFrame jFrame = new JFrame("登陆界面");

Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 200, 150);

jFrame.setResizable(false);

jFrame.setLayout(null);

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label1 = new JLabel("姓名");

label1.setBounds(10, 10, 100, 30);

jFrame.add(label1);

JLabel label2 = new JLabel("密码");

label2.setBounds(10, 40, 100, 30);

jFrame.add(label2);

final JTextField text1 = new JTextField();

text1.setBounds(50, 15, 130, 20);

jFrame.add(text1);

final JPasswordField text2 = new JPasswordField();

text2.setBounds(50, 45, 130, 20);

jFrame.add(text2);

JButton button = new JButton("Login");

button.setBounds(10, 75, 170, 40);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if(userName.equals(text1.getText())  passwrod.equals(text2.getText())) {

JOptionPane.showMessageDialog(null, "登陆成功误", "提示", JOptionPane.INFORMATION_MESSAGE);

} else {

JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);

text1.setText("");

text2.setText("");

}

}

});

jFrame.add(button);

jFrame.setVisible(true);

}

}

我有一个微信公众号,经常会分享一些Java技术相关的干货,还有一些学习资源。

如果你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注。

Java如何登录token的代码是什么?

首先是Token主类。类很简单

package com.company.util;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

public class Token {

private static final String TOKEN_LIST_NAME = "tokenList";

public static final String TOKEN_STRING_NAME = "token";

private static ArrayList getTokenList(HttpSession session) {

Object obj = session.getAttribute(TOKEN_LIST_NAME);

if (obj != null) {

return (ArrayList) obj;

} else {

ArrayList tokenList = new ArrayList();

session.setAttribute(TOKEN_LIST_NAME, tokenList);

return tokenList;

}

}

private static void saveTokenString(String tokenStr, HttpSession session) {

ArrayList tokenList = getTokenList(session);

tokenList.add(tokenStr);

session.setAttribute(TOKEN_LIST_NAME, tokenList);

}

private static String generateTokenString(){

return new Long(System.currentTimeMillis()).toString();

}

/** *//**

* Generate a token string, and save the string in session, then return the token string.

*

* @param HttpSession

* session

* @return a token string used for enforcing a single request for a particular transaction.

*/

public static String getTokenString(HttpSession session) {

String tokenStr = generateTokenString();

saveTokenString(tokenStr, session);

return tokenStr;

}

/** *//**

* check whether token string is valid. if session contains the token string, return true.

* otherwise, return false.

*

* @param String

* tokenStr

* @param HttpSession

* session

* @return true: session contains tokenStr; false: session is null or tokenStr is id not in session

*/

public static boolean isTokenStringValid(String tokenStr, HttpSession session) {

boolean valid = false;

if(session != null){

ArrayList tokenList = getTokenList(session);

if (tokenList.contains(tokenStr)) {

valid = true;

tokenList.remove(tokenStr);

}

}

return valid;

}

}

在jsp页面端。

首先import该类:

%@ page import="com.company.util.Token" %

表单包含隐藏的token字符串:

form

input type="hidden" name="%=Token.TOKEN_STRING_NAME %" value="%=Token.getTokenString(session) %"

/form

在Servlet中添加如下代码。

if(Token.isTokenStringValid(request.getParameter(Token.TOKEN_STRING_NAME), request.getSession())){

//进行正常业务流程

求JAVA实现用户登录界面代码?

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

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 JComboBoxString 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();

}

}


分享标题:java简单登录代码,java简单代码运行
浏览地址:http://cdkjz.cn/article/hcoidh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220