Java實(shí)現(xiàn)空指針后的猜拳游戲_第1頁
Java實(shí)現(xiàn)空指針后的猜拳游戲_第2頁
Java實(shí)現(xiàn)空指針后的猜拳游戲_第3頁
Java實(shí)現(xiàn)空指針后的猜拳游戲_第4頁
Java實(shí)現(xiàn)空指針后的猜拳游戲_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Java實(shí)現(xiàn)空指針后的猜拳游戲目錄前言插件實(shí)現(xiàn)創(chuàng)建項(xiàng)目猜拳游戲?qū)崿F(xiàn)游戲彈窗實(shí)現(xiàn)監(jiān)聽空指針異常安裝插件演示最后

前言

java.lang.NullPointerException空指針異??梢哉f是Java程序最容易出現(xiàn)的異常了,我寫了一個(gè)IDEA插件,每當(dāng)程序出現(xiàn)空指針異常時(shí)就會(huì)彈出一個(gè)猜拳游戲窗口,該窗口不能直接關(guān)閉,只有當(dāng)你游戲獲勝時(shí),窗口才會(huì)自動(dòng)關(guān)閉。

作用是啥?

嘲諷罷了。

插件實(shí)現(xiàn)

創(chuàng)建項(xiàng)目

IDEA創(chuàng)建一個(gè)插件開發(fā)項(xiàng)目非常方便,已經(jīng)內(nèi)置了。

猜拳游戲?qū)崿F(xiàn)

很簡單。

實(shí)現(xiàn)原理:提供3個(gè)按鈕,分別為石頭、剪刀、布,對(duì)應(yīng)值1、2、3,再為按鈕綁定點(diǎn)擊事件,按鍵點(diǎn)擊之后調(diào)用處理函數(shù)傳入對(duì)應(yīng)的值即可。

處理函數(shù)handle(intselectedValue)的實(shí)現(xiàn):利用隨機(jī)數(shù)隨機(jī)為電腦生成一個(gè)值與用戶選擇的值做比較,石頭贏剪刀、剪刀贏布、布贏石頭,然后顯示游戲結(jié)果,用戶獲勝時(shí)會(huì)觸發(fā)回調(diào)函數(shù)(用于關(guān)閉彈窗)。

packagecn.xeblog.mora.ui;

importjavax.swing.*;

importjava.awt.*;

importjava.util.Random;

*@authoranlingyi

*@date2025/8/118:02PM

publicclassMoraGameextendsJPanel{

*猜拳獲勝調(diào)用

privateRunnablerunnable;

*提示標(biāo)簽

privateJLabeltipsLabel;

*結(jié)束標(biāo)記

privatebooleanisOver;

publicMoraGame(Runnablerunnable){

setMinimumSize(newDimension(250,100));

setLayout(newBorderLayout());

this.runnable=runnable;

init();

privatevoidinit(){

this.tipsLabel=newJLabel("請(qǐng)出拳!",JLabel.CENTER);

this.tipsLabel.setPreferredSize(newDimension(250,50));

this.tipsLabel.setFont(newFont("",0,15));

this.tipsLabel.setForeground(newColor(255,128,128));

JButtonstoneButton=newJButton("石頭");

JButtonshearsButton=newJButton("剪刀");

JButtonclothButton=newJButton("布");

stoneButton.setFocusPainted(false);

stoneButton.setBorderPainted(false);

stoneButton.addActionListener(l-handle(1));

shearsButton.setFocusPainted(false);

shearsButton.setBorderPainted(false);

shearsButton.addActionListener(l-handle(2));

clothButton.setFocusPainted(false);

clothButton.setBorderPainted(false);

clothButton.addActionListener(l-handle(3));

JPanelcenterPanel=newJPanel();

centerPanel.setPreferredSize(newDimension(250,30));

centerPanel.add(stoneButton);

centerPanel.add(shearsButton);

centerPanel.add(clothButton);

add(tipsLabel,BorderLayout.NORTH);

add(centerPanel,BorderLayout.CENTER);

privatevoidhandle(intselectedValue){

if(isOver){

return;

intvalue=newRandom().nextInt(3)+1;

booleanisWin=selectedValue==(value-1==03:value-1);

Stringresult;

if(isWin){

isOver=true;

result="你贏~";

}elseif(selectedValue==value){

result="平局~";

}else{

result="電腦贏~";

showTips("電腦-"+getText(value)+",你-"+getText(selectedValue)+","+result);

if(isWin){

newThread(()-{

try{

Thread.sleep(800);

this.runnable.run();

}catch(InterruptedExceptione){

thrownewRuntimeException(e);

}).start();

privateStringgetText(intvalue){

switch(value){

case1:

return"石頭";

case2:

return"剪刀";

case3:

return"布";

return"";

privatevoidshowTips(Stringtips){

tipsLabel.setText(tips);

}

游戲彈窗實(shí)現(xiàn)

將窗口設(shè)置為不可關(guān)閉,傳遞彈窗關(guān)閉回調(diào)函數(shù)到游戲處理對(duì)象。

packagecn.xeblog.mora.ui;

importellij.openapi.ui.DialogWrapper;

importorg.jetbrains.annotations.NotNull;

importorg.jetbrains.annotations.Nullable;

importjavax.swing.*;

*@authoranlingyi

*@date2025/8/117:58PM

publicclassMoraDialogextendsDialogWrapper{

publicMoraDialog(){

super(true);

setTitle("猜拳游戲?");

setResizable(false);

setCrossClosesWindow(false);

init();

@Override

protected@NullableJComponentcreateCenterPanel(){

returnnewMoraGame(()-SwingUtilities.invokeLater(()-this.close(0)));

@Override

protected@NotNullAction[]createActions(){

returnnewAction[]{};

}

監(jiān)聽空指針異常

實(shí)現(xiàn)控制臺(tái)過濾接口,判斷控制臺(tái)的輸出內(nèi)容是否包含java.lang.NullPointerException,如果包含則彈出游戲窗口。

packagecn.xeblog.mora.filter;

importcn.xeblog.mora.ui.MoraDialog;

importellij.execution.filters.ConsoleFilterProvider;

importellij.execution.filters.Filter;

importject.Project;

importorg.jetbrains.annotations.NotNull;

importjavax.swing.*;

*@authoranlingyi

*@date2025/8/1111:46PM

publicclassConsoleFilterimplementsConsoleFilterProvider{

@Override

publicFilter@NotNull[]getDefaultFilters(@NotNullProjectproject){

returnnewFilter[]{(line,entireLength)-{

if(line.contains("java.lang.NullPointerException")){

SwingUtilities.invokeLater(()-newMoraDialog().show());

returnnull;

}

注冊(cè)過濾器

plugin.xml添加我們自定義的控制臺(tái)過濾器實(shí)現(xiàn)。

extensionsdefaultExtensionNs="ellij"

consoleFilterProviderimplementation="cn.xeblog.mora.filter.ConsoleFilter"/

/extensions

安裝插件

插件打包

Gradle-Tasks-build-assemble

打包之后的文件位于項(xiàng)目的build目錄下:build/distributions/xxx.zip

插件安裝

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論