import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class BackgroundPanel extends JPanel
{
private static final long serialVersionUID = 8657137422032250507L;
private Image image = null;
public BackgroundPanel(Image image)
{
this.image = image;
}
protected void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
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.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
public class Main
{
private static String getPath()
{
File[] fileArr = new File("C:\\Users\\22x\\AppData\\Roaming\\Microsoft\\Windows\\Themes\\CachedFiles")
.listFiles();// win10下的用户设置的壁纸
File maxSize = fileArr[0];
for (File Temp : fileArr)
{
if (Temp.length() > maxSize.length())
maxSize = Temp;
}
return maxSize.getPath();// 返回最大文件的路径,即最高清晰度
}
public static void main(String[] args)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕大小
JFrame jframe = new JFrame();
jframe.setLayout(null);
jframe.setUndecorated(true);// 无边框
Rectangle bounds = new Rectangle(screenSize);
jframe.setBounds(bounds);
Image image = new ImageIcon(getPath()).getImage();
JPanel jp = new BackgroundPanel(image);// 添加JPanel,在JPanel上添加图片
jp.setLayout(null);
jp.setBounds(jframe.getBounds());
jframe.add(jp);
JButton exit = new JButton("退出");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
exit.setBounds(0, 0, 100, 30);
exit.setVisible(true);// 退出按钮默认可见
jp.add(exit);
JPasswordField jtf = new JPasswordField();// 密码输入
jtf.setBounds((screenSize.width / 2) - 100, screenSize.height - 100, 200, 30);
jtf.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)// 如按了指定的键,则关机
{
if (17 == e.getKeyCode() || 18 == e.getKeyCode() || 110 == e.getKeyCode() || 115 == e.getKeyCode()
|| 192 == e.getKeyCode())
{
try
{
Runtime.getRuntime().exec("shutdown /s /t " + 0);
} catch (IOException e1)
{
e1.printStackTrace();
}
}
}
});
jp.add(jtf);
JButton lockButton = new JButton("锁定");
lockButton.addActionListener(new ActionListener()
{
boolean lockFlag = false;
String password = null;
public void actionPerformed(ActionEvent arg0)
{
if (true == lockFlag)
{
String input = new String(jtf.getPassword());
if (input.equals(password))
{
lockFlag = false;
lockButton.setText("锁定");
jtf.setText("");
exit.setVisible(true);
} else
{
JOptionPane.showMessageDialog(jtf, "密码错误", "提示", 0);
jtf.setText("");
}
} else
{
String input = new String(jtf.getPassword());
if (false == input.equals(""))
{
password = input;
lockFlag = true;
jtf.setText("");
lockButton.setText("解锁");
exit.setVisible(false);
} else
{
JOptionPane.showMessageDialog(jtf, "请输入密码", "提示", 1);
}
}
}
});
lockButton.setBounds((screenSize.width / 2) - 50, screenSize.height - 50, 100, 30);
jp.add(lockButton);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}
Java 锁屏软件
源码附上
原文作者:java锁
原文地址: https://blog.csdn.net/kyaa111/article/details/80548334
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/kyaa111/article/details/80548334
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。