问题描述
importjavax.swing.*;importjava.awt.event.*;publicclassLottoEventimplementsItemListener,ActionListener,Runnable{LottoMadnessgui;Threadplaying;publicLottoEvent(LottoMadnessin){gui=in;}publicvoidactionPerformed(ActionEventevent){Stringcommand=event.getActionCommand();if(command=="Play"){startPlaying();}if(command=="Stop"){stopPlaying();}if(command=="Reset"){clearAllFields();}}voidstartPlaying(){playing=newThread(this);playing.start();gui.play.setEnabled(false);gui.stop.setEnabled(true);gui.reset.setEnabled(false);gui.quickpick.setEnabled(false);gui.personal.setEnabled(false);}voidstopPlaying(){gui.stop.setEnabled(false);gui.play.setEnabled(true);gui.reset.setEnabled(true);gui.quickpick.setEnabled(true);gui.personal.setEnabled(true);playing=null;}voidclearAllFields(){for(inti=0;i<6;i++){gui.numbers[i].setText(null);gui.winners[i].setText(null);}gui.got3.setText("0");gui.got4.setText("0");gui.got5.setText("0");gui.got6.setText("0");gui.drawings.setText("0");gui.years.setText(null);}publicvoiditemStateChanged(ItemEventevent){Objectitem=event.getItem();if(item==gui.quickpick){for(inti=0;i<6;i++){intpick;do{pick=(int)Math.floor(Math.random()*50+1);}while(numberGone(pick,gui.numbers,i));gui.numbers[i].setText(""+pick);}}else{for(inti=0;i<6;i++){gui.numbers[i].setText(null);}}}voidaddOneToField(JTextFieldfield){intnum=Integer.parseInt("0"+field.getText());num++;field.setText(""+num);}booleannumberGone(intnum,JTextField[]pastNums,intcount){for(inti=0;i<count;i++){if(Integer.parseInt(pastNums[i].getText())==num){returntrue;}}returnfalse;}booleanmatchedOne(JTextFieldwin,JTextField[]allPicks){for(inti=0;i<6;i++){StringwinText=win.getText();if(winText.equals(allPicks[i].getText())){returntrue;}}returnfalse;}publicvoidrun(){ThreadthisThread=Thread.currentThread();while(playing==thisThread){addOneToField(gui.drawings);intdraw=Integer.parseInt(gui.drawings.getText());floatnumYears=(float)draw/104;gui.years.setText(""+numYears);intmatches=0;for(inti=0;i<6;i++){intball;do{ball=(int)Math.floor(Math.random()*50+1);}while(numberGone(ball,gui.winners,i));gui.winners[i].setText(""+ball);if(matchedOne(gui.winners[i],gui.numbers)){matches++;}}switch(matches){case3:addOneToField(gui.got3);break;case4:addOneToField(gui.got4);break;case5:addOneToField(gui.got5);break;case6:addOneToField(gui.got6);gui.stop.setEnabled(false);gui.play.setEnabled(true);playing=null;}try{Thread.sleep(100);}catch(InterruptedExceptione){//donothing}}}}
importjava.awt.*;importjavax.swing.*;publicclassLottoMadnessextendsJFrame{LottoEventlotto=newLottoEvent(this);//setuprow1JPanelrow1=newJPanel();ButtonGroupoption=newButtonGroup();JCheckBoxquickpick=newJCheckBox("QuickPick",false);JCheckBoxpersonal=newJCheckBox("Personal",true);//setuprow2JPanelrow2=newJPanel();JLabelnumbersLabel=newJLabel("Yourpicks:",JLabel.RIGHT);JTextField[]numbers=newJTextField[6];JLabelwinnersLabel=newJLabel("Winners:",JLabel.RIGHT);JTextField[]winners=newJTextField[6];//setuprow3JPanelrow3=newJPanel();JButtonstop=newJButton("Stop");JButtonplay=newJButton("Play");JButtonreset=newJButton("Reset");//setuprow4JPanelrow4=newJPanel();JLabelgot3Label=newJLabel("3of6:",JLabel.RIGHT);JTextFieldgot3=newJTextField("0");JLabelgot4Label=newJLabel("4of6:",JLabel.RIGHT);JTextFieldgot4=newJTextField("0");JLabelgot5Label=newJLabel("5of6:",JLabel.RIGHT);JTextFieldgot5=newJTextField("0");JLabelgot6Label=newJLabel("6of6:",JLabel.RIGHT);JTextFieldgot6=newJTextField("0",10);JLabeldrawingsLabel=newJLabel("Drawings:",JLabel.RIGHT);JTextFielddrawings=newJTextField("0");JLabelyearsLabel=newJLabel("Years:",JLabel.RIGHT);JTextFieldyears=newJTextField();publicLottoMadness(){super("LottoMadness");setSize(550,270);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);GridLayoutlayout=newGridLayout(5,1,10,10);setLayout(layout);//Addlistenersquickpick.addItemListener(lotto);personal.addItemListener(lotto);stop.addActionListener(lotto);play.addActionListener(lotto);reset.addActionListener(lotto);FlowLayoutlayout1=newFlowLayout(FlowLayout.CENTER,10,10);option.add(quickpick);option.add(personal);row1.setLayout(layout1);row1.add(quickpick);row1.add(personal);add(row1);GridLayoutlayout2=newGridLayout(2,7,10,10);row2.setLayout(layout2);row2.add(numbersLabel);for(inti=0;i<6;i++){numbers[i]=newJTextField();row2.add(numbers[i]);}row2.add(winnersLabel);for(inti=0;i<6;i++){winners[i]=newJTextField();winners[i].setEditable(false);row2.add(winners[i]);}add(row2);FlowLayoutlayout3=newFlowLayout(FlowLayout.CENTER,10,10);row3.setLayout(layout3);stop.setEnabled(false);row3.add(stop);row3.add(play);row3.add(reset);add(row3);GridLayoutlayout4=newGridLayout(2,3,20,10);row4.setLayout(layout4);row4.add(got3Label);got3.setEditable(false);row4.add(got3);row4.add(got4Label);got4.setEditable(false);row4.add(got4);row4.add(got5Label);got5.setEditable(false);row4.add(got5);row4.add(got6Label);got6.setEditable(false);row4.add(got6);row4.add(drawingsLabel);drawings.setEditable(false);row4.add(drawings);row4.add(yearsLabel);years.setEditable(false);row4.add(years);add(row4);setVisible(true);}publicstaticvoidmain(String[]arguments){LottoMadnessframe=newLottoMadness();}}
请问第一个程序中:第8行的Threadplaying什么意思?第10行的(LottoMadnessin)这个用法怎么的?第28行,还是Thread。。有没有大虾给一个注册解释什么的啊。。。书上的程序没解释,好郁闷纳。谢谢大家。
解决方案
解决方案二:
LottoMadness应该是你继承自JFrame的一个类吧就是下面那个in就是这个类型的一个对象跟inta;是一个意思。Thread是线程,具体用法我也不是太清楚,可以去专门找资料学一下~
解决方案三:
好牛逼的样子,这还刚学?开什么玩笑!!1
解决方案四:
Threadplaying;就是新定义了一个线程;与这段代码playing=newThread(this);结合就是新建一个线程,因为java支持多线程的并发任务;等同于Threadplaying=newThread(this);