2024年10月java代码大全(求一计算器java代码,最好简单一点(图形界面))

 更新时间:2024-10-12

  ⑴java代码大全(求一计算器java代码,最好简单一点(图形界面

  ⑵不一条条注释if((x==x&&(y==y+||y==y-))||((x==x+||x==x-)&&(y==y))){//判断是否相邻remove();}else{for(j=;j《;j++){if(grid==){//判断第一个按钮同行哪个按钮为空if(y》j){//如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for(i=y-;i》=j;i--){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid!=){k=;break;}else{k=;}//K=说明通过了第一次验证}if(k==){linePassOne();}}if(y《j){//如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for(i=y+;i《=j;i++){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid!=){k=;break;}else{k=;}}if(k==){linePassOne();}}if(y==j){linePassOne();}}if(k==){if(x==x){remove();}if(x《x){for(n=x;n《=x-;n++){if(grid!=){k=;break;}if(grid==&&n==x-){remove();}}}if(x》x){for(n=x;n》=x+;n--){if(grid!=){k=;break;}if(grid==&&n==x+){remove();}}}}}for(i=;i《;i++){//列if(grid==){if(x》i){for(j=x-;j》=i;j--){if(grid!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x《i){for(j=x+;j《=i;j++){if(grid!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x==i){rowPassOne();}}if(k==){if(y==y){remove();}if(y《y){for(n=y;n《=y-;n++){if(grid!=){k=;break;}if(grid==&&n==y-){remove();}}}if(y》y){for(n=y;n》=y+;n--){if(grid!=){k=;break;}if(grid==&&n==y+){remove();}}}}}}}publicvoidlinePassOne(){if(y》j){//第一按钮同行空按钮在左边for(i=y-;i》=j;i--){//判断第一按钮同左侧空按钮之间有没按钮if(grid!=){k=;break;}else{k=;}//K=说明通过了第二次验证}}if(y《j){//第一按钮同行空按钮在与第二按钮之间for(i=y+;i《=j;i++){if(grid!=){k=;break;}else{k=;}}}}publicvoidrowPassOne(){if(x》i){for(j=x-;j》=i;j--){if(grid!=){k=;break;}else{k=;}}}if(x《i){for(j=x+;j《=i;j++){if(grid!=){k=;break;}else{k=;}}}}publicvoidremove(){firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation=false;k=;grid=;grid=;}publicvoidactionPerformed(ActionEvente){if(e.getSource()==newlyButton){intgrid;this.grid=grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();}if(e.getSource()==exitButton)System.exit();if(e.getSource()==resetButton)reload();for(intcols=;cols《;cols++){for(introws=;rows《;rows++){if(e.getSource()==diamondsButton)estimateEven(cols+,rows+,diamondsButton);}}}publicstaticvoidmain(Stringargs){lianliankanllk=newlianliankan();llk.randomBuild();llk.init();}}//oldlines//newlines一段最简单的java代码程序有哪些publilassHelloWorld{publicstaticvoidmain(Stringargs){System.out.println(“helloworld!“);}}一.基本概念Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,重新开始按钮JLabelfractionLable=newJLabel(““);//分数标签JButtonfirstButton,secondButton;//分别记录两次被选中的按钮intgrid;//储存游戏按钮位置staticbooleanpressInformation=false;//判断是否有按钮被选中intx=,y=,x=,y=,fristMsg=,secondMsg=,validateLV;//游戏按钮的位置坐标inti,j,k,n;//消除方法控制publicvoidinit(){mainFrame=newJFrame(“JKJ连连看“);thisContainer=mainFrame.getContentPane();thisContainer.setLayout(newBorderLayout());centerPanel=newJPanel();southPanel=newJPanel();northPanel=newJPanel();thisContainer.add(centerPanel,“Center“);thisContainer.add(southPanel,“South“);thisContainer.add(northPanel,“North“);centerPanel.setLayout(newGridLayout(,));for(intcols=;cols《;cols++){for(introws=;rows《;rows++){diamondsButton));diamondsButton.addActionListener(this);centerPanel.add(diamondsButton);}}exitButton=newJButton(“退出“);exitButton.addActionListener(this);resetButton=newJButton(“重列“);resetButton.addActionListener(this);newlyButton=newJButton(“再来一局“);newlyButton.addActionListener(this);southPanel.add(exitButton);southPanel.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));northPanel.add(fractionLable);mainFrame.setBounds(,,,);mainFrame.setVisible(true);}publicvoidrandomBuild(){intrandoms,cols,rows;for(inttwins=;twins《=;twins++){randoms=(int)(Math.random()*+);for(intalike=;alike《=;alike++){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid=randoms;}}}publicvoidfraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+));}publicvoidreload(){intsave;intn=,cols,rows;intgrid;for(inti=;i《=;i++){for(intj=;j《=;j++){if(this.grid!=){save;n++;}}}n=n-;this.grid=grid;while(n》=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid;n--;}mainFrame.setVisible(false);pressInformation=false;//这里一定要将按钮点击信息归为初始init();for(inti=;i《;i++){for(intj=;j《;j++){if(grid==)diamondsButton.setVisible(false);}}}publicvoidestimateEven(intplaceX,intplaceY,JButtonbz){if(pressInformation==false){x=placeX;y=placeY;secondMsg=grid;secondButton=bz;pressInformation=true;}else{x=x;y=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid;secondButton=bz;if(fristMsg==secondMsg&&secondButton!=firstButton){xiao();}}}publicvoidxiao(){//相同的情况下能不能消去,最好简单一点(图形界面importjava.awt.*;importjava.awt.event.*;importjava.lang.*;importjavax.swing.*;publilassCounterextendsFrame{//声明三个面板的布局GridLayoutgl,gl,gl;Panelp,p,p,p;JTextFieldtf;TextFieldtf;Buttonb,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b;StringBufferstr;//显示屏所显示的字符串doublex,y;//x和y都是运算数intz;//Z表示单击了那一个运算符.表示“+“,表示“-“,表示“*“,表示“/“staticdoublem;//记忆的数字publiounter(){gl=newGridLayout(,,,);//实例化三个面板的布局gl=newGridLayout(,,,);gl=newGridLayout(,,,);tf=newJTextField();//显示屏tf.setHorizontalAlignment(JTextField.RIGHT);tf.setEnabled(false);tf.setText(““);tf=newTextField();//显示记忆的索引值tf.setEditable(false);//实例化所有按钮、设置其前景色并注册监听器b=newButton(“Backspace“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“CE“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“C“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“MC“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“MR“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“MS“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“M+“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“/“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“sqrt“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“*“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“%“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“-“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“/X“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“+/-“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“.“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“+“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“=“);b.setForeground(Color.red);b.addActionListener(newBt());//实例化四个面板p=newPanel();p=newPanel();p=newPanel();p=newPanel();//创建一个空字符串缓冲区str=newStringBuffer();//添加面板p中的组件和设置其在框架中的位置和大小p.add(tf);p.setBounds(,,,);//添加面板p中的组件和设置其在框架中的位置和大小p.setLayout(gl);p.add(tf);p.add(b);p.add(b);p.add(b);p.setBounds(,,,);//添加面板p中的组件并设置其的框架中的位置和大小p.setLayout(gl);p.add(b);p.add(b);p.add(b);p.add(b);p.setBounds(,,,);//添加面板p中的组件并设置其在框架中的位置和大小p.setLayout(gl);//设置p的布局p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.setBounds(,,,);//设置框架中的布局为空布局并添加个面板setLayout(null);add(p);add(p);add(p);add(p);setResizable(false);//禁止调整框架的大小//匿名类关闭窗口addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit();}});setBackground(Color.lightGray);setBounds(,,,);setVisible(true);}//构造监听器classBtimplementsActionListener{publicvoidactionPerformed(ActionEvente){try{if(e.getSource()==b)//选择“CE“清零{tf.setText(““);//把显示屏清零str.setLength();//清空字符串缓冲区以准备接收新的输入运算数}elseif(e.getSource()==b)//选择“C“清零{tf.setText(““);//把显示屏清零str.setLength();}elseif(e.getSource()==b)//单击“+/-“选择输入的运算数是正数还是负数{x=Double.parseDouble(tf.getText().trim());tf.setText(““+(-x));}elseif(e.getSource()==b)//单击加号按钮获得x的值和z的值并清空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();//清空缓冲区以便接收新的另一个运算数y=d;z=;}elseif(e.getSource()==b)//单击减号按钮获得x的值和z的值并清空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();y=d;z=;}elseif(e.getSource()==b)//单击乘号按钮获得x的值和z的值并清空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();y=d;z=;}elseif(e.getSource()==b)//单击除号按钮获得x的值和z的值并空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();y=d;z=;}elseif(e.getSource()==b)//单击等号按钮输出计算结果{str.setLength();switch(z){case:tf.setText(““+(x+y));break;case:tf.setText(““+(x-y));break;case:tf.setText(““+(x*y));break;case:tf.setText(““+(x/y));break;}}elseif(e.getSource()==b)//单击“.“按钮输入小数{if(tf.getText().trim().indexOf(′.′)!=-)//判断字符串中是否已经包含了小数点{}else//如果没数点有小{if(tf.getText().trim().equals(““))//如果初时显示为{str.setLength();tf.setText((str.append(““+e.getActionmand())).toString());}elseif(tf.getText().trim().equals(““))//如果初时显示为空则不做任何操作{}else{tf.setText(str.append(e.getActionmand()).toString());}}y=d;}elseif(e.getSource()==b)//求平方根{x=Double.parseDouble(tf.getText().trim());tf.setText(“数字格式异常“);if(x《)tf.setText(“负数没有平方根“);elsetf.setText(““+Math.sqrt(x));str.setLength();y=d;}elseif(e.getSource()==b)//单击了“%“按钮{x=Double.parseDouble(tf.getText().trim());tf.setText(““+(.*x));str.setLength();y=d;}elseif(e.getSource()==b)//单击了“/X“按钮{x=Double.parseDouble(tf.getText().trim());if(x==){tf.setText(“除数不能为零“);}else{tf.setText(““+(/x));}str.setLength();y=d;}elseif(e.getSource()==b)//MC为清除内存{m=d;tf.setText(““);str.setLength();}elseif(e.getSource()==b)//MR为重新调用存储的数据{if(tf.getText().trim()!=““)//有记忆数字{tf.setText(““+m);}}elseif(e.getSource()==b)//MS为存储显示的数据{m=Double.parseDouble(tf.getText().trim());tf.setText(“M“);tf.setText(““);str.setLength();}elseif(e.getSource()==b)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR{m=m+Double.parseDouble(tf.getText().trim());}else//选择的是其他的按钮{if(e.getSource()==b)//如果选择的是““这个数字键{if(tf.getText().trim().equals(““))//如果显示屏显示的为零不做操作{}else{tf.setText(str.append(e.getActionmand()).toString());y=Double.parseDouble(tf.getText().trim());}}elseif(e.getSource()==b)//选择的是“BackSpace”按钮{if(!tf.getText().trim().equals(““))//如果显示屏显示的不是零{if(str.length()!=){tf.setText(str.delete(str.length()-,str.length()).toString());//可能抛出字符串越界异常}else{tf.setText(““);str.setLength();}}y=Double.parseDouble(tf.getText().trim());}else//其他的数字键{tf.setText(str.append(e.getActionmand()).toString());y=Double.parseDouble(tf.getText().trim());}}}catch(NumberFormatExceptione){tf.setText(“数字格式异常“);}catch(StringIndexOutOfBoundsExceptione){tf.setText(“字符串索引越界“);}}}publicstaticvoidmain(Stringargs){newCounter();}}求一个简单的Java小游戏的代码连连看的小源码packageLianliankan;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publilasslianliankanimplementsActionListener{JFramemainFrame;//主面板ContainerthisContainer;JPanelcenterPanel,southPanel,northPanel;//子面板JButtondiamondsButton;//游戏按钮数组JButtonexitButton,resetButton,newlyButton;//退出,JavaPlatformMicroEdition,最好简单一点(图形界面求一个简单的Java小游戏的代码一段最简单的java代码程序有哪些求一计算器java代码,JavaPlatform,EnterpriseEdition,标准版,企业版。

  ⑶求一计算器java代码,最好简单一点(图形界面

  ⑷importjava.awt.*;importjava.awt.event.*;importjava.lang.*;importjavax.swing.*;publilassCounterextendsFrame{//声明三个面板的布局GridLayoutgl,gl,gl;Panelp,p,p,p;JTextFieldtf;TextFieldtf;Buttonb,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b;StringBufferstr;//显示屏所显示的字符串doublex,y;//x和y都是运算数intz;//Z表示单击了那一个运算符.表示“+“,表示“-“,表示“*“,表示“/“staticdoublem;//记忆的数字publiounter(){gl=newGridLayout(,,,);//实例化三个面板的布局gl=newGridLayout(,,,);gl=newGridLayout(,,,);tf=newJTextField();//显示屏tf.setHorizontalAlignment(JTextField.RIGHT);tf.setEnabled(false);tf.setText(““);tf=newTextField();//显示记忆的索引值tf.setEditable(false);//实例化所有按钮、设置其前景色并注册监听器b=newButton(“Backspace“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“CE“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“C“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“MC“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“MR“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“MS“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“M+“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“/“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“sqrt“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“*“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“%“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“-“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“/X“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(““);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“+/-“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“.“);b.setForeground(Color.blue);b.addActionListener(newBt());b=newButton(“+“);b.setForeground(Color.red);b.addActionListener(newBt());b=newButton(“=“);b.setForeground(Color.red);b.addActionListener(newBt());//实例化四个面板p=newPanel();p=newPanel();p=newPanel();p=newPanel();//创建一个空字符串缓冲区str=newStringBuffer();//添加面板p中的组件和设置其在框架中的位置和大小p.add(tf);p.setBounds(,,,);//添加面板p中的组件和设置其在框架中的位置和大小p.setLayout(gl);p.add(tf);p.add(b);p.add(b);p.add(b);p.setBounds(,,,);//添加面板p中的组件并设置其的框架中的位置和大小p.setLayout(gl);p.add(b);p.add(b);p.add(b);p.add(b);p.setBounds(,,,);//添加面板p中的组件并设置其在框架中的位置和大小p.setLayout(gl);//设置p的布局p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.add(b);p.setBounds(,,,);//设置框架中的布局为空布局并添加个面板setLayout(null);add(p);add(p);add(p);add(p);setResizable(false);//禁止调整框架的大小//匿名类关闭窗口addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit();}});setBackground(Color.lightGray);setBounds(,,,);setVisible(true);}//构造监听器classBtimplementsActionListener{publicvoidactionPerformed(ActionEvente){try{if(e.getSource()==b)//选择“CE“清零{tf.setText(““);//把显示屏清零str.setLength();//清空字符串缓冲区以准备接收新的输入运算数}elseif(e.getSource()==b)//选择“C“清零{tf.setText(““);//把显示屏清零str.setLength();}elseif(e.getSource()==b)//单击“+/-“选择输入的运算数是正数还是负数{x=Double.parseDouble(tf.getText().trim());tf.setText(““+(-x));}elseif(e.getSource()==b)//单击加号按钮获得x的值和z的值并清空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();//清空缓冲区以便接收新的另一个运算数y=d;z=;}elseif(e.getSource()==b)//单击减号按钮获得x的值和z的值并清空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();y=d;z=;}elseif(e.getSource()==b)//单击乘号按钮获得x的值和z的值并清空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();y=d;z=;}elseif(e.getSource()==b)//单击除号按钮获得x的值和z的值并空y的值{x=Double.parseDouble(tf.getText().trim());str.setLength();y=d;z=;}elseif(e.getSource()==b)//单击等号按钮输出计算结果{str.setLength();switch(z){case:tf.setText(““+(x+y));break;case:tf.setText(““+(x-y));break;case:tf.setText(““+(x*y));break;case:tf.setText(““+(x/y));break;}}elseif(e.getSource()==b)//单击“.“按钮输入小数{if(tf.getText().trim().indexOf(′.′)!=-)//判断字符串中是否已经包含了小数点{}else//如果没数点有小{if(tf.getText().trim().equals(““))//如果初时显示为{str.setLength();tf.setText((str.append(““+e.getActionmand())).toString());}elseif(tf.getText().trim().equals(““))//如果初时显示为空则不做任何操作{}else{tf.setText(str.append(e.getActionmand()).toString());}}y=d;}elseif(e.getSource()==b)//求平方根{x=Double.parseDouble(tf.getText().trim());tf.setText(“数字格式异常“);if(x《)tf.setText(“负数没有平方根“);elsetf.setText(““+Math.sqrt(x));str.setLength();y=d;}elseif(e.getSource()==b)//单击了“%“按钮{x=Double.parseDouble(tf.getText().trim());tf.setText(““+(.*x));str.setLength();y=d;}elseif(e.getSource()==b)//单击了“/X“按钮{x=Double.parseDouble(tf.getText().trim());if(x==){tf.setText(“除数不能为零“);}else{tf.setText(““+(/x));}str.setLength();y=d;}elseif(e.getSource()==b)//MC为清除内存{m=d;tf.setText(““);str.setLength();}elseif(e.getSource()==b)//MR为重新调用存储的数据{if(tf.getText().trim()!=““)//有记忆数字{tf.setText(““+m);}}elseif(e.getSource()==b)//MS为存储显示的数据{m=Double.parseDouble(tf.getText().trim());tf.setText(“M“);tf.setText(““);str.setLength();}elseif(e.getSource()==b)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR{m=m+Double.parseDouble(tf.getText().trim());}else//选择的是其他的按钮{if(e.getSource()==b)//如果选择的是““这个数字键{if(tf.getText().trim().equals(““))//如果显示屏显示的为零不做操作{}else{tf.setText(str.append(e.getActionmand()).toString());y=Double.parseDouble(tf.getText().trim());}}elseif(e.getSource()==b)//选择的是“BackSpace”按钮{if(!tf.getText().trim().equals(““))//如果显示屏显示的不是零{if(str.length()!=){tf.setText(str.delete(str.length()-,str.length()).toString());//可能抛出字符串越界异常}else{tf.setText(““);str.setLength();}}y=Double.parseDouble(tf.getText().trim());}else//其他的数字键{tf.setText(str.append(e.getActionmand()).toString());y=Double.parseDouble(tf.getText().trim());}}}catch(NumberFormatExceptione){tf.setText(“数字格式异常“);}catch(StringIndexOutOfBoundsExceptione){tf.setText(“字符串索引越界“);}}}publicstaticvoidmain(Stringargs){newCounter();}}

  ⑸求一个简单的Java小游戏的代码

  ⑹连连看的小源码packageLianliankan;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publilasslianliankanimplementsActionListener{JFramemainFrame;//主面板ContainerthisContainer;JPanelcenterPanel,southPanel,northPanel;//子面板JButtondiamondsButton;//游戏按钮数组JButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮JLabelfractionLable=newJLabel(““);//分数标签JButtonfirstButton,secondButton;//分别记录两次被选中的按钮intgrid;//储存游戏按钮位置staticbooleanpressInformation=false;//判断是否有按钮被选中intx=,y=,x=,y=,fristMsg=,secondMsg=,validateLV;//游戏按钮的位置坐标inti,j,k,n;//消除方法控制publicvoidinit(){mainFrame=newJFrame(“JKJ连连看“);thisContainer=mainFrame.getContentPane();thisContainer.setLayout(newBorderLayout());centerPanel=newJPanel();southPanel=newJPanel();northPanel=newJPanel();thisContainer.add(centerPanel,“Center“);thisContainer.add(southPanel,“South“);thisContainer.add(northPanel,“North“);centerPanel.setLayout(newGridLayout(,));for(intcols=;cols《;cols++){for(introws=;rows《;rows++){diamondsButton));diamondsButton.addActionListener(this);centerPanel.add(diamondsButton);}}exitButton=newJButton(“退出“);exitButton.addActionListener(this);resetButton=newJButton(“重列“);resetButton.addActionListener(this);newlyButton=newJButton(“再来一局“);newlyButton.addActionListener(this);southPanel.add(exitButton);southPanel.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));northPanel.add(fractionLable);mainFrame.setBounds(,,,);mainFrame.setVisible(true);}publicvoidrandomBuild(){intrandoms,cols,rows;for(inttwins=;twins《=;twins++){randoms=(int)(Math.random()*+);for(intalike=;alike《=;alike++){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid=randoms;}}}publicvoidfraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+));}publicvoidreload(){intsave;intn=,cols,rows;intgrid;for(inti=;i《=;i++){for(intj=;j《=;j++){if(this.grid!=){save;n++;}}}n=n-;this.grid=grid;while(n》=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid;n--;}mainFrame.setVisible(false);pressInformation=false;//这里一定要将按钮点击信息归为初始init();for(inti=;i《;i++){for(intj=;j《;j++){if(grid==)diamondsButton.setVisible(false);}}}publicvoidestimateEven(intplaceX,intplaceY,JButtonbz){if(pressInformation==false){x=placeX;y=placeY;secondMsg=grid;secondButton=bz;pressInformation=true;}else{x=x;y=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid;secondButton=bz;if(fristMsg==secondMsg&&secondButton!=firstButton){xiao();}}}publicvoidxiao(){//相同的情况下能不能消去。仔细分析,不一条条注释if((x==x&&(y==y+||y==y-))||((x==x+||x==x-)&&(y==y))){//判断是否相邻remove();}else{for(j=;j《;j++){if(grid==){//判断第一个按钮同行哪个按钮为空if(y》j){//如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for(i=y-;i》=j;i--){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid!=){k=;break;}else{k=;}//K=说明通过了第一次验证}if(k==){linePassOne();}}if(y《j){//如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for(i=y+;i《=j;i++){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid!=){k=;break;}else{k=;}}if(k==){linePassOne();}}if(y==j){linePassOne();}}if(k==){if(x==x){remove();}if(x《x){for(n=x;n《=x-;n++){if(grid!=){k=;break;}if(grid==&&n==x-){remove();}}}if(x》x){for(n=x;n》=x+;n--){if(grid!=){k=;break;}if(grid==&&n==x+){remove();}}}}}for(i=;i《;i++){//列if(grid==){if(x》i){for(j=x-;j》=i;j--){if(grid!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x《i){for(j=x+;j《=i;j++){if(grid!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x==i){rowPassOne();}}if(k==){if(y==y){remove();}if(y《y){for(n=y;n《=y-;n++){if(grid!=){k=;break;}if(grid==&&n==y-){remove();}}}if(y》y){for(n=y;n》=y+;n--){if(grid!=){k=;break;}if(grid==&&n==y+){remove();}}}}}}}publicvoidlinePassOne(){if(y》j){//第一按钮同行空按钮在左边for(i=y-;i》=j;i--){//判断第一按钮同左侧空按钮之间有没按钮if(grid!=){k=;break;}else{k=;}//K=说明通过了第二次验证}}if(y《j){//第一按钮同行空按钮在与第二按钮之间for(i=y+;i《=j;i++){if(grid!=){k=;break;}else{k=;}}}}publicvoidrowPassOne(){if(x》i){for(j=x-;j》=i;j--){if(grid!=){k=;break;}else{k=;}}}if(x《i){for(j=x+;j《=i;j++){if(grid!=){k=;break;}else{k=;}}}}publicvoidremove(){firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation=false;k=;grid=;grid=;}publicvoidactionPerformed(ActionEvente){if(e.getSource()==newlyButton){intgrid;this.grid=grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();}if(e.getSource()==exitButton)System.exit();if(e.getSource()==resetButton)reload();for(intcols=;cols《;cols++){for(introws=;rows《;rows++){if(e.getSource()==diamondsButton)estimateEven(cols+,rows+,diamondsButton);}}}publicstaticvoidmain(Stringargs){lianliankanllk=newlianliankan();llk.randomBuild();llk.init();}}//oldlines//newlines

  ⑺一段最简单的java代码程序有哪些

  ⑻publilassHelloWorld{

  ⑼publicstaticvoidmain(Stringargs){

  ⑽System.out.println(“helloworld!“);

  ⑾Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

  ⑿Java分为三个体系,分别为JavaSE(JSE,JavaPlatformStandardEdition,标准版,

  ⒀JavaEE(JEE,JavaPlatform,EnterpriseEdition,企业版。

  ⒁JavaME(JME,JavaPlatformMicroEdition,微型版。

您可能感兴趣的文章:

相关文章