程序改好了你在OpenJFrame这个类中画界面就行了, ML 这个类是一个监听功能,给你的按钮加上了监听,另外建议不要再main函数中写界面的代码 .
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){
Panel pan1 = new Panel();
Label label1 = new Label("欢迎登录**超市薪酬管理系统");
Button bt1 = new Button("登录");
pan1.setBackground(Color.gray);
frm.setLayout(border);
frm.setBackground(Color.gray);
pan1.add(label1);
frm.add(pan1,BorderLayout.NORTH);
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{
System.out.println("我是注册 我要关闭当前页面 然后打开其他页面");
class OpenJFrame extends JFrame{
public OpenJFrame(){
//设置此窗口永远为最上方 是window的方法
this.setAlwaysOnTop(true);
//不允许用户改变窗口的大小
this.setResizable(false);
//让窗口在屏幕的正中间显示
this.setLocationRelativeTo(null);
this.setTitle("新的窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
自己写的比较规范的代码,都有注释:
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.setResizable(false);//设置框架不可以改变大小
this.setTitle("用户登录");//设置框架标题
this.pnluser.setLayout(null);//设置面板布局管理
this.pnluser.setBackground(Color.cyan);//设置面板背景颜色
this.lbluserLogIn.setText("用户登录");//设置标签标题
this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色
this.lbluserName.setText("用户名:");
this.lbluserPWD.setText("密 码:");
this.btnSub.setText("登录");
this.btnReset.setText("重置");
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
);
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
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);
}else if(true){
this.dispose();
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
new UserLogIn();
这两行代码中的字符串, 内容不一致 . 为了避免这种情况,我们可以定义一个常量字符串,进行使用
修改后的效果图
完整的参考代码
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?search?extends?JFrame?implements?ActionListener?{
JLabel?jl1;
ButtonGroup?bg1?=?null;
//?为了避免前后使用的字符串不一致?,?定义两个字符串常量
static?final?String?OK_CMD="?确定?";
static?final?String?EXIT_CMD="?返回?";
public?search()?{
jp1?=?new?JPanel();
bg1?=?new?ButtonGroup();
jb1?=?new?JButton(OK_CMD);
jl1?=?new?JLabel("查询功能");
jrb1?=?new?JRadioButton("?查看余额?");
jp1.setBackground(Color.YELLOW);
jrb1.setBackground(Color.YELLOW);
bg1.add(jrb1);
jp1.add(jl1);
this.add(jp1);
this.setTitle("查询功能");?
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?
this.setResizable(true);
setLocationRelativeTo(null);
//?jrb1.setSelected(true);?//一般会有一个单选按钮处于选中状态
this.setVisible(true);?//?一般布局完成,最后才调用显示方法
jb1.addActionListener(this);
@Override?//?这里的事件监听是有问题的
public?void?actionPerformed(ActionEvent?e)?{?//
String?cmd?=?e.getActionCommand();
if?(OK_CMD.equals(cmd))?{?//?字符串内容是否相等?,强烈推荐使用equals方法,?不要使用?==!!!
if?(jrb1.isSelected())?{
System.out.println("进行查找功能!!!");
System.out.println("进行读取信息功能!!!");
System.out.println("先按功能咯!!!");
}?else?if?(EXIT_CMD.equals(cmd))?{
System.out.println("退出啦!!!");
//添加一个main方法用于测试
public?static?void?main(String[]?args)?{
new?search();
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;
final?String?userName?=?"abc";
JFrame?jFrame?=?new?JFrame("登陆界面");
Dimension?dimension?=?Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel?label1?=?new?JLabel("姓名");
jFrame.add(label1);
final?JTextField?text1?=?new?JTextField();
jFrame.add(text1);
JButton?button?=?new?JButton("Login");
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
JOptionPane.showMessageDialog(null,?"登陆成功误",?"提示",?JOptionPane.INFORMATION_MESSAGE);
}?else?{
JOptionPane.showMessageDialog(null,?"错误",?"提示",?JOptionPane.ERROR_MESSAGE);
text1.setText("");
});
jFrame.add(button);
jFrame.setVisible(true);
我有一个微信公众号,经常会分享一些Java技术相关的干货,还有一些学习资源.
如果你喜欢我的分享,可以用微信搜索"Java团长"或者"javatuanzhang"关注.
用java做QQ登录界面的写法如下:
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
①.、//定义该类继承自JFrame,实现ActionListener接口
public class LoginTest extends JFrame implements ActionListener
private JPanel jp=new JPanel();
JLabel name = new JLabel("请输入用户名");
JLabel password = new JLabel("请输入密码");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
JButton login = new JButton("登陆");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
jp.setLayout(null);
jp.add(jl[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
①.0、//添加文本框到JPanel容器中
jp.add(jName);
①.1、//为文本框注册动作事件监听器
jName.addActionListener(this);
jp.add(jPassword);
jPassword.setEchoChar('$');
jPassword.addActionListener(this);
this.add(jp);
this.setTitle("登陆窗口");
public void actionPerformed(ActionEvent e)
if(e.getSource()==jName)
jPassword.requestFocus();
else if(e.getSource()==jb[1])
jName.setText("");
jPassword.setText("");
jName.requestFocus();
else
if(jName.getText().equals("lixiangguo")
public static void main(String[] args)
new LoginTest();
第一段:监听域对象中属性的变更的监听器
域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器. ?
这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同.
①1、attributeAdded 方法
当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象
各个域属性监听器中的完整语法定义为:
public void attributeAdded(ServletContextAttributeEvent scae)
public void attributeReplaced(HttpSessionBindingEvent hsbe)
public void attributeRmoved(ServletRequestAttributeEvent srae)
当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应
public void attributeRemoved(ServletContextAttributeEvent scae)
public void attributeRemoved (HttpSessionBindingEvent hsbe)
public void attributeRemoved (ServletRequestAttributeEvent srae)
当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应
public void attributeReplaced(ServletContextAttributeEvent scae)
public void attributeReplaced (HttpSessionBindingEvent hsbe)
public void attributeReplaced (ServletRequestAttributeEvent srae)
编写ServletContextAttributeListener监听器监听ServletContext域对象的属性值变化情况,代码如下:
package me.gacl.web.listener;
import java.text.MessageFormat;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
/**
* @ClassName: MyServletContextAttributeListener
* @Description: ServletContext域对象中属性的变更的事件监听器
* @author: 孤傲苍狼
*
*/
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中添加了属性:{0},属性值是:{1}"
,scab.getName()
,scab.getValue());
System.out.println(str);
public void attributeRemoved(ServletContextAttributeEvent scab) {
"ServletContext域对象中删除属性:{0},属性值是:{1}"
public void attributeReplaced(ServletContextAttributeEvent scab) {
"ServletContext域对象中替换了属性:{0}的值"
,scab.getName());
在web.xml文件中注册监听器
listener
descriptionMyServletContextAttributeListener监听器/description
listener-classme.gacl.web.listener.MyServletContextAttributeListener/listener-class
/listener
编写ServletContextAttributeListenerTest.jsp测试页面
!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登录界面监听代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!