Java登陆界面

这是我第一次写博客,有什么写的不是很好请见谅啊,嘻嘻。

下面进入主题,这写的是个Java界面编程的登陆界面,加了事件处理,输入密码后可以成功跳转,为了界面更美观,我加了个图片作为背景。下面展示图片及代码,

package login;


import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.*;
import java.io.File;
import java.io.IOException;

import javax.swing.*;

public class Login extends JFrame implements ActionListener {	
		  //private static final String JPanel = null;
	    JPanel jp=new JPanel();
	 
		  JTextField fieldAccount = new JTextField(1000);
		 
		  JPasswordField fieldPassword = new JPasswordField(1000);
		 JButton jb1 = new JButton("登 录");
		 
		  JButton jb2=new JButton("退出");
		  
		  JLabel label = new JLabel("  帐  号   ");
		  JLabel label1 = new JLabel(" 密 码   ");
		  ImageIcon img1=new ImageIcon("src/12.jpg");
		  JLabel label0 = new JLabel(img1);
//		  public void paint(Graphics g){
//				 super.paint(g);
//				 ImageIcon imge=new ImageIcon("src/12.jpg");
//				 imge.setImage(imge.getImage().getScaledInstance(this.getWidth(),this.getHeight(),Image.SCALE_AREA_AVERAGING));
//			g.drawImage(imge.getImage(),10,10,null);
//		
//			}
//		
	  

public Login(){
			this.setTitle("用户登录界面.....");
			this.setBounds(500, 130, 750, 850);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setLayout(null);
			this.setVisible(true);
		    //this.setResizable(false);
			//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			//this.setLayout(new GridLayout(3, 1));//网格式布局
			jb2.addActionListener(this);	
			  
		  
		    jb1.setBackground(new Color(233,240,247));
	        jb1.setForeground(new Color(30,57,91));
	        jb2.setBackground(new Color(233,240,247));
	        jb2.setForeground(new Color(30,57,91));

			
			

			JLabel label = new JLabel("账号");
		  
		    label.setOpaque(false);
			label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
			label.setBounds(150,140,100,50);
			label.setForeground(Color.white);
			label.setFont(new Font("微软雅黑",Font.PLAIN,18));
		    
			//jb1.setForeground(Color.white);
			jb1.setFont(new Font("微软雅黑",Font.PLAIN,18));
			//jb2.setForeground(Color.white);
			jb2.setFont(new Font("微软雅黑",Font.PLAIN,18));
		    
			 fieldAccount.setOpaque(false);
			 fieldAccount.setBorder(null);
			 fieldPassword.setOpaque(false);
			 fieldPassword.setBorder(null);
			jb1.setOpaque(false);
			jb2.setOpaque(false);
			jb1.setBorder(null);
			 jb2.setBorder(null);
			//label.setBackground(Color.red);
			//label.setForeground(Color.red);
		
			this.add(label);
			fieldAccount.setBounds(250, 140, 300, 50);
			this.add(fieldAccount);
			
			label1 = new JLabel("  密 码   ");
			
			label1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
			label1.setForeground(Color.white);
			label1.setFont(new Font("微软雅黑",Font.PLAIN,18));
		    
			//label1.setForeground(Color.red);
			label1.setBounds(145, 238, 100, 50);
			this.add(label1);
			fieldPassword.setBounds(250, 240, 300, 50);
			this.add(fieldPassword);
			
		
			this.add(jb1);
			this.add(jb2);
			
			
			
			jb1.setBounds(250,350,100,50);
			jb2.setBounds(450,350,100,50);
			jb1.addActionListener(this);
			
			this.setVisible(true);
			//jb1.setEnabled(true);
           //jb2.setEnabled(true);
          
			// pwd 监听enter键
			fieldPassword.addActionListener(this);	
			
		

  
   }
public void paint(Graphics g){
	 super.paint(g);
	 ImageIcon imge=new ImageIcon("src/15.jpg");
	 imge.setImage(imge.getImage().getScaledInstance(this.getWidth(),this.getHeight(),Image.SCALE_AREA_AVERAGING));
    //g.drawImage(imge.getImage(),10,10,null);
	g.drawImage(imge.getImage(),10,10,null);
}


		//@Override
		public void actionPerformed(ActionEvent e) {
			if( e.getSource().equals(jb2))
	        {
				this.dispose();
	           // System.exit( 0 );

		       
			}
			if(e.getSource() == fieldPassword|| e.getSource() == jb1){
				// 获取帐号
				String account = fieldAccount.getText();
				// 获取密码
				String pwd = new String(fieldPassword.getPassword());
				if (account.compareTo("123") == 0 && pwd.compareTo("123") == 0){
					JOptionPane.showMessageDialog(null, "登录成功");
					this.dispose();
					TiaoZhuan tz=new TiaoZhuan();
				//Login_Photo window = new Login_Photo();
				//window.setVisible(true); 
				//this.dispose();
				
				}
				
		        
			if(account.compareTo("123")!=0||pwd.compareTo("123")!=0){
				JOptionPane.showMessageDialog(null, "登录失败" + "\n" + "id:123" + "\n" + "password:123");
			}
				//else {JOptionPane.showMessageDialog(null, "登录失败" + "\n" + "id:123" + "\n" + "password:123");
					
			//}
		
		}
		}

	
public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Login();
	}
}



喜欢的可以加个关注哦

发布了15 篇原创文章 · 获赞 9 · 访问量 3120

猜你喜欢

转载自blog.csdn.net/qq_44716544/article/details/100862979