资讯

精准传达 • 有效沟通

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

java登录界面监听代码 用java实现登录界面

Java用户界面设计:怎样为按钮设置监听,在点击按钮的时候,弹出一个新的窗口

程序改好了你在OpenJFrame这个类中画界面就行了, ML 这个类是一个监听功能,给你的按钮加上了监听,另外建议不要再main函数中写界面的代码 。

创新互联公司专注于企业成都营销网站建设、网站重做改版、镇宁网站定制设计、自适应品牌网站建设、H5响应式网站成都做商城网站、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为镇宁等各大城市提供网站开发制作服务。

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

import java.awt.Frame;

import java.awt.Label;

import java.awt.Panel;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;import javax.swing.JLabel;

import javax.swing.JPanel;

public class bysj{

static Frame frm = new Frame("中小型超市薪酬管理系统");

public static void main(String[] args){

BorderLayout border = new BorderLayout(5,10);

//GridLayout grid = new GridLayout(2,2);

Panel pan1 = new Panel();

pan1.setSize(500,150);

Panel pan2 = new Panel();

pan2.setSize(500,150);

Panel pan3 = new Panel();

pan3.setSize(500,150);

Label label1 = new Label("欢迎登录**超市薪酬管理系统");

label1.setSize(500,50);

Label label2 = new Label("用户名:");

label2.setLocation(10,10);

label2.setSize(30,50);

Label label3 = new Label("密码:");

label3.setLocation(10,70);

label3.setSize(30,50);

Button bt1 = new Button("登录");

bt1.setBounds(40,70,100,40); bt1.addActionListener(new ML());

Button bt2 = new Button("注册");

bt2.setBounds(150,70,100,40); bt2.addActionListener(new ML());

TextField tf1 = new TextField("",20);

tf1.setBounds(50, 10, 50, 50);

TextField tf2 = new TextField("",20);

tf2.setEchoChar('*');

tf2.setBounds(10, 70, 30, 50);

pan1.setBackground(Color.gray);

pan2.setBackground(Color.gray);

//pan2.setLayout(grid);

pan3.setBackground(Color.gray);

frm.setLayout(border);

frm.setSize(600,400);

frm.setBackground(Color.gray);

frm.setLocation(350,100);

pan1.add(label1);

pan2.add(label2);

pan2.add(tf1);

pan2.add(label3);

pan2.add(tf2);

pan3.add(bt1);

pan3.add(bt2);

frm.add(pan1,BorderLayout.NORTH);

frm.add(pan2,BorderLayout.CENTER);

frm.add(pan3,BorderLayout.SOUTH);

frm.setVisible(true);

frm.addWindowListener(new WinCloser());

}

public static class WinCloser extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}}

class ML implements ActionListener{ public void actionPerformed(ActionEvent event){

Button eventobj = (Button)event.getSource();

String labelname = eventobj.getLabel();

if("登录".equals(labelname)){

new bysj().frm.dispose();

new OpenJFrame();

System.out.println("我是登录 我要关闭当前页面 然后打开其他页面");

}else{

new bysj().frm.dispose();

new OpenJFrame();

System.out.println("我是注册 我要关闭当前页面 然后打开其他页面");

}

}

}

class OpenJFrame extends JFrame{

public OpenJFrame(){

JPanel jp2 = new JPanel(new BorderLayout());

this.setSize(380,245);

//设置此窗口永远为最上方 是window的方法

this.setAlwaysOnTop(true);

//不允许用户改变窗口的大小

this.setResizable(false);

//让窗口在屏幕的正中间显示

this.setLocationRelativeTo(null);

this.setTitle("新的窗口");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

}

java实现简单登录界面

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

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图形界面事件监听的一些小问题

错误一: jb2=new JButton(" 返回 ");   if(e.getActionCommand()==" 退出 ")

这两行代码中的字符串, 内容不一致 . 为了避免这种情况,我们可以定义一个常量字符串,进行使用

错误二: 事件的触发是点击两个按钮, 跟jrb1和jrb2没有关系的,所以只需要给jb1和jb2添加事件响应

修改后的效果图

完整的参考代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class search extends JFrame implements ActionListener {

JPanel jp1, jp2, jp3;

JLabel jl1;

JButton jb1, jb2;

JRadioButton jrb1, jrb2 = null;

ButtonGroup bg1 = null;

// 为了避免前后使用的字符串不一致 , 定义两个字符串常量

static final String OK_CMD=" 确定 ";

static final String EXIT_CMD=" 返回 ";

public search() {

jp1 = new JPanel();

jp2 = new JPanel();

jp3 = new JPanel();

bg1 = new ButtonGroup();

jb1 = new JButton(OK_CMD);

jb2 = new JButton(EXIT_CMD);

jl1 = new JLabel("查询功能");

jl1.setFont(new Font("宋体", Font.PLAIN, 24));

jrb1 = new JRadioButton(" 查看余额 ");

jrb2 = new JRadioButton(" 基本信息 "); 

jb1.setFont(new Font("宋体", Font.PLAIN, 16));

jb2.setFont(new Font("宋体", Font.PLAIN, 16));

jrb1.setFont(new Font("宋体", Font.PLAIN, 16));

jrb2.setFont(new Font("宋体", Font.PLAIN, 16)); 

jp1.setBackground(Color.YELLOW);

jp2.setBackground(Color.YELLOW);

jp3.setBackground(Color.YELLOW);

jrb1.setBackground(Color.YELLOW);

jrb2.setBackground(Color.YELLOW);

bg1.add(jrb1);

bg1.add(jrb2);

jp1.add(jl1);

jp2.add(jrb1);

jp2.add(jrb2);

jp3.add(jb1);

jp3.add(jb2); 

this.add(jp1);

this.add(jp2);

this.add(jp3); 

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

this.setTitle("查询功能"); 

this.setSize(400, 350); 

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

this.setResizable(true);

setLocationRelativeTo(null);

// jrb1.setSelected(true); //一般会有一个单选按钮处于选中状态

this.setVisible(true); // 一般布局完成,最后才调用显示方法

jb1.addActionListener(this);

jb2.addActionListener(this);

}

@Override // 这里的事件监听是有问题的

public void actionPerformed(ActionEvent e) { //

String cmd = e.getActionCommand();

if (OK_CMD.equals(cmd)) { // 字符串内容是否相等 ,强烈推荐使用equals方法, 不要使用 ==!!!

// 点击了确定按钮,接下来判断 单选按钮的选中情况

if (jrb1.isSelected()) {

System.out.println("进行查找功能!!!");

} else if (jrb2.isSelected()) {

System.out.println("进行读取信息功能!!!");

} else {// 如果jrb1和jrb2都没有选择

System.out.println("先按功能咯!!!");

}

} else if (EXIT_CMD.equals(cmd)) {

System.out.println("退出啦!!!");

}

}

//添加一个main方法用于测试

public static void main(String[] args) {

new search();

}

}

用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怎么实现QQ登录界面?

用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=0;i2;i++)

{

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

}

}

如何用java实现自动监听处理jsp页面的

一、监听域对象中属性的变更的监听器

域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。  

这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

1.1、attributeAdded 方法

当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

各个域属性监听器中的完整语法定义为:

public void attributeAdded(ServletContextAttributeEvent scae)

public void attributeReplaced(HttpSessionBindingEvent hsbe)

public void attributeRmoved(ServletRequestAttributeEvent srae)

1.2、attributeRemoved 方法

当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应

各个域属性监听器中的完整语法定义为:

public void attributeRemoved(ServletContextAttributeEvent scae)

public void attributeRemoved (HttpSessionBindingEvent hsbe)

public void attributeRemoved (ServletRequestAttributeEvent srae)

1.3、attributeReplaced 方法

当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应

各个域属性监听器中的完整语法定义为:

public void attributeReplaced(ServletContextAttributeEvent scae)

public void attributeReplaced (HttpSessionBindingEvent hsbe)

public void attributeReplaced (ServletRequestAttributeEvent srae)

1.4、ServletContextAttributeListener监听器范例:

编写ServletContextAttributeListener监听器监听ServletContext域对象的属性值变化情况,代码如下:

package me.gacl.web.listener;

import java.text.MessageFormat;

import javax.servlet.ServletContextAttributeEvent;

import javax.servlet.ServletContextAttributeListener;

/**

* @ClassName: MyServletContextAttributeListener

* @Description: ServletContext域对象中属性的变更的事件监听器

* @author: 孤傲苍狼

* @date: 2014-9-11 下午10:53:04

*

*/

public class MyServletContextAttributeListener implements

ServletContextAttributeListener {

@Override

public void attributeAdded(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中添加了属性:{0},属性值是:{1}"

,scab.getName()

,scab.getValue());

System.out.println(str);

}

@Override

public void attributeRemoved(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中删除属性:{0},属性值是:{1}"

,scab.getName()

,scab.getValue());

System.out.println(str);

}

@Override

public void attributeReplaced(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中替换了属性:{0}的值"

,scab.getName());

System.out.println(str);

}

}

在web.xml文件中注册监听器

listener

descriptionMyServletContextAttributeListener监听器/description

listener-classme.gacl.web.listener.MyServletContextAttributeListener/listener-class

/listener

编写ServletContextAttributeListenerTest.jsp测试页面

%@ page language="java" pageEncoding="UTF-8"%

!DOCTYPE HTML

html

head

titleServletContextAttributeListener监听器测试/title

/head

body

%

//往application域对象中添加属性

application.setAttribute("name", "孤傲苍狼");

//替换application域对象中name属性的值

application.setAttribute("name", "gacl");

//移除application域对象中name属性

application.removeAttribute("name");

%

/body

/html

运行结果如下:

从运行结果中可以看到,ServletContextListener监听器成功监听到了ServletContext域对象(application)中的属性值的变化情况。


新闻标题:java登录界面监听代码 用java实现登录界面
转载来于:http://cdkjz.cn/article/hehodd.html
多年建站经验

多一份参考,总有益处

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

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

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