微信公开课小程序热门讨论「急需一些java小程序代码」最新回复-importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassCalculatorextendsJFrame{privateContainercontainer;privateGridBagLayoutlayout;privateGridBagConstraintsconstraints;privateJTextFielddisplayField;privateStringlastCommand;privatedoubleresult;privatebooleanstart;publicCalculator(){super(Calculator);container=getContentPane();layout=newGridBagLayout();container.setLayout(layout);constraints=newGridBagConstraints();start=true;result=0;lastCommand==;displayField=newJTextField(20);displayField.setHorizontalAlignment(JTextField.RIGHT);constraints.gridx=0;constraints.gridy=0;constraints.gridwidth=4;constraints.gridheight=1;constraints.fill=GridBagConstraints.BOTH;constraints.weightx=100;constraints.weighty=100;layout.setConstraints(displayField,constraints);container.add(displayField);ActionListenerinsert=newInsertAction();ActionListenercommand=newCommandAction();addButton(Backspace,0,1,2,1,insert);addButton(CE,2,1,1,1,insert);addButton(C,3,1,1,1,insert);addButton(7,0,2,1,1,insert);addButton(8,1,2,1,1,insert);addButton(9,2,2,1,1,insert);addButton(/,3,2,1,1,command);addButton(4,0,3,1,1,insert);addButton(5,1,3,1,1,insert);addButton(6,2,3,1,1,insert);addButton(*,3,3,1,1,command);addButton(1,0,4,1,1,insert);addButton(2,1,4,1,1,insert);addButton(3,2,4,1,1,insert);addButton(,3,4,1,1,command);addButton(0,0,5,1,1,insert);addButton(+/,1,5,1,1,insert);addButton(.,2,5,1,1,insert);addButton(+,3,5,1,1,command);addButton(=,0,6,4,1,command);setSize(300,300);setVisible(true);}privatevoidaddButton(Stringlabel,introw,intcolumn,intwith,intheight,ActionListenerlistener){JButtonbutton=newJButton(label);constraints.gridx=row;constraints.gridy=column;constraints.gridwidth=with;constraints.gridheight=height;constraints.fill=GridBagConstraints.BOTH;button.addActionListener(listener);layout.setConstraints(button,constraints);container.add(button);}privateclassInsertActionimplementsActionListener{publicvoidactionPerformed(ActionEventevent){Stringinput=event.getActionCommand();if(start){displayField.setText();start=false;if(input.equals(+/))displayField.setText(displayField.getText()+);}if(!input.equals(+/)){if(input.equals(Backspace)){Stringstr=displayField.getText();if(str.length()0)displayField.setText(str.substring(0,str.length()1));}elseif(input.equals(CE)||input.equals(C)){displayField.setText(0);start=true;}elsedisplayField.setText(displayField.getText()+input);}}}privateclassCommandActionimplementsActionListener{publicvoidactionPerformed(ActionEventevt){Stringcommand=evt.getActionCommand();if(start){lastCommand=command;}else{calculate(Double.parseDouble(displayField.getText()));lastCommand=command;start=true;}}}publicvoidcalculate(doublex){if(lastCommand.equals(+))result+=x;elseif(lastCommand.equals())result=x;elseif(lastCommand.equals(*))result*=x;elseif(lastCommand.equals(/))result/=x;elseif(lastCommand.equals(=))result=x;displayField.setText(+result);}publicstaticvoidmain(String[]args){Calculatorcalculator=newCalculator();calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}
21点游戏代码:importjava.awt.Color;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.SwingConstants;importjavax.swing.border.LineBorder;publicclassgame21extendsJFrame{privateJLabellabel_2;privateintnumber;privateintsum;finalJLabellabel=newJLabel();finalJLabellabel_1=newJLabel();publicstaticvoidmain(String[]args){newgame21();}publicgame21(){super(21点?!);getContentPane().setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);finalJButtonbutton=newJButton();button.addActionListener(newActionListener(){publicvoidactionPerformed(finalActionEventarg0){onClick();}});button.setText(出牌);button.setBounds(170,350,106,28);getContentPane().add(button);label.setBorder(newLineBorder(Color.black,1,false));label.setHorizontalAlignment(SwingConstants.CENTER);label.setFont(newFont(,Font.BOLD,26));label.setText(背面);label.setBounds(158,81,137,153);getContentPane().add(label);label_1.setText(你已经拥有的牌:);label_1.setBounds(109,22,270,45);getContentPane().add(label_1);this.setBounds(200,300,501,528);this.setVisible(true);getContentPane().add(getLabel_2());}publicintrandNumber(){try{Thread.sleep(10);}catch(InterruptedExceptione){e.printStackTrace();}return(int)(Math.random()*10+1);}publicvoidonClick(){number=this.randNumber();this.sum+=number;label.setText(+number);StringstrTemp=this.label_1.getText();strTemp+=+number+;label_1.setText(strTemp);Stringtemp=合计:+sum;label_2.setText(temp);isWin();}publicvoidisWin(){if(sum21){JOptionPane.showMessageDialog(this,你输了);clear();return;}elseif(sum==21){JOptionPane.showMessageDialog(this,你赢了);clear();return;}else{inti=JOptionPane.showOptionDialog(this,是否继续?,提示,JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);if(i==JOptionPane.OK_OPTION){onClick();}elsereturn;}}privatevoidclear(){label_2.setText(合计:);sum=0;number=0;label_1.setText(你已经拥有的牌:);}/***@return*/protectedJLabelgetLabel_2(){if(label_2==null){label_2=newJLabel();label_2.setText(合计:);label_2.setBounds(313,35,66,18);}returnlabel_2;}}都是编译成功的java源代码,希望对你有帮助! 更多有关「急需一些java小程序代码」的疑问请扫码关注微信公开课+小程序!了解更多