Java代码的登录界面

Java代码的简单登录界面
package loginwindow;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.xml.transform.Source;
public class MyWindow<JButtonListener> extends JFrame{
      Button jbLogin,jbExit;//登录,退出按钮
      JLabel a1,a2,a3;//标签
      JTextField b;//文本框
      JPasswordField pw;//密码框
      public MyWindow(){
    	  Container con=super.getContentPane();//顶级容器
    	  con.setLayout(null);//空布局
    	  
    	  a1=new JLabel("登录");
    	  a1.setBounds(225,10,100,100);
    	  
    	  a2=new JLabel("用户名:");
    	  a2.setBounds(100,85,100,60);//设置组件的位置,大小(x,y,width, height)
    	  b=new JTextField(20);
    	  b.setBounds(180,100,200,30);
    	  b.setBackground(Color.CYAN);//设置颜色
 
    	  a3=new JLabel("密    码:");
    	  a3.setBounds(100,145,100,60);
    	  pw=new JPasswordField(20);
    	  pw.setBounds(180,160,200,30);
    	  pw.setBackground(Color.CYAN);
    	
    	  jbLogin=new JButton("登录");
    	  jbLogin.setBounds(200,250,60,40);
    	  jbLogin.setBackground(Color.CYAN);
    	  jbExit=new JButton("退出");
    	  jbExit.setBounds(300,250,60,40);
    	  jbExit.setBackground(Color.CYAN);
    	  con.add(a1);
    	  con.add(a2);//把组件添加到界面上
    	  con.add(b);
    	  con.add(a3);
    	  con.add(pw);
    	  con.add(jbLogin);
    	  con.add(jbExit);
    	  super.setBackground(Color.BLUE);
    	  super.setSize(500, 400);//设置窗口大小
    	  super.setVisible(true);//设置窗口为可见的
       	  super.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭按钮 
       	  
       	  ButtonListener lis=new ButtonListener();
       	  jbExit.addActionListener( lis);//注册监听事件
       	  jbLogin.addActionListener(lis);
      }
      
     //main函数      
      public static void main(String [] args){
    	  MyWindow mw=new MyWindow();
      }
     //事件监听器接口
      class  ButtonListener implements ActionListener{
	  public void actionPerformed(ActionEvent e){
		Object source=e.getSource();
		if(source instanceof JButton ){
		    JButton jb=(JButton)source;//按钮
		    String txt=jb.getText();
		    String userName=b.getText();//用户名
		    String password=pw.getText();//密码
		    if(txt.equals("登录")){
		        if(userName.equals("123")&&password.equals("123456")){					
			    JOptionPane.showMessageDialog(null, "登陆成功!!");//提示
		        }else{
			    JOptionPane.showMessageDialog(null, "用户名或密码错误!!");
			}
		    }else if(txt.equals("退出")){
			System.exit(0);
		    }
		}	
	  }
      }
  }


             


  • 16
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值