package com.hua.app;
import javax.swing.*;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.Timer;
public class LockView extends JDialog implements ActionListener,MouseListener{
JPasswordField jtf;
JLabel jlTitle;
JPanel jp1,jp2;
JButton jbtn1,jbtn2,jbtn3;
String pwd="";
boolean flag=false; //指示鼠标是否移出
Robot rb;
String osPath;
private boolean ctrl_Pressed=false;
private boolean alt_Pressed=false;
private boolean del_Pressed=false;
BackImage bi=null;
public LockView(){
jbtn3=new JButton("关闭");
jbtn3.setEnabled(true);
jbtn3.addActionListener(this);
this.setUndecorated(true);
osPath=System.getenv("SystemRoot");
osPath=osPath+"\\system32\\";
this.addMouseListener(this);
jbtn1=new JButton("更改密码");
this.setAlwaysOnTop(false);
jbtn1.addActionListener(this);
jbtn2=new JButton("加锁");
jbtn2.addActionListener(this);
jp2=new JPanel(new FlowLayout(FlowLayout.CENTER));
jp2.add(jbtn1);
jp2.add(jbtn2);
jp2.add(jbtn3);
jp1=new JPanel();
jtf=new JPasswordField(10);
jlTitle=new JLabel("请输入密码:");
jp1.setLayout(new FlowLayout(FlowLayout.CENTER));
jp1.add(jlTitle);
jp1.add(jtf);
this.setLayout(new BorderLayout());
this.add(jp1,"North");
this.add(jp2,"Center");
this.setTitle("ScreenLock");
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((width-300)/2,(height-100)/2);
this.setSize(300,100);
this.setVisible(true);
this.setResizable(false);
this.jtf.setEditable(false);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
try {
rb=new Robot(screen);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new LockView();
Timer t;
t = new Timer(true);
t.schedule(
new java.util.TimerTask() { public void run() { closeMgr(); }}, 0, 1000);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbtn1){
UpdatePwd up=new UpdatePwd();
pwd=up.getPwd();
System.out.println(pwd);
jbtn1.setEnabled(false);
}else if(e.getSource()==jbtn2&& jbtn2.getText().equals("加锁")){
jbtn3.setEnabled(false);
this.jtf.setEditable(true);
this.jbtn2.setText("解锁");
bi=new BackImage();
this.setAlwaysOnTop(true);
try {
Runtime.getRuntime().exec("taskkill /f /im explorer.exe");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
flag=true;
}else if(e.getSource()==jbtn2&& jbtn2.getText().equals("解锁")){
jbtn3.setEnabled(true);
char[] r=jtf.getPassword();
String s="";
for(int i=0;i<r.length;i++){
s=s+r[i];
}
System.out.println(s);
if(s.equals(pwd)){
//解锁成功
flag=false;
try {
Runtime.getRuntime().exec("cmd /c start explorer.exe");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jbtn2.setText("加锁");
bi.dispose();
this.setAlwaysOnTop(false);
}else{
//解锁失败
JOptionPane.showMessageDialog(this, "密码错误");
}
}else if(e.getSource()==jbtn3){
this.dispose();
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
if(flag==true)
{
if(this.getMousePosition()==null)
{
resetPos(e);
}
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
private void resetPos(MouseEvent e){
int curX=this.getX()+e.getX();
int curY=this.getY()+e.getY();
int frameLeft = this.getX()+20;
int frameRight = this.getX()+this.getWidth()-20;
int frameUp = this.getY()+80;
int frameDown = this.getY()+this.getHeight()-20;
int x = curX;
int y = curY;
if(curX<frameLeft){
x = frameLeft;
rb.mouseMove(x, y);
}
if(curX>frameRight){
x = frameRight;
rb.mouseMove(x, y);
}
if(curY<frameUp){
y = frameUp;
rb.mouseMove(x, y);
}
if(curY>frameDown){
y = frameDown;
rb.mouseMove(x, y);
}
if(x!=curX || y!=curY)
rb.mouseMove(x, y);
}
private static void closeMgr(){
try {
Runtime.getRuntime().exec("taskkill /f /im taskmgr.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}