java猜数字游戏源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

importjava.awt.Color;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.Box;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;classWindowButtonextendsJFrameimplementsActionListener{/****/privatestaticfinallongserialVersionUID=1L;intnumber;JTextFieldinputNumber;JLabelfeedBack;JButtonbuttonGetNumber,buttonEnter;WindowButton(Strings){super(猜数字小游戏);buttonGetNumber=newJButton(得到一个随机数);feedBack=newJLabel(无反馈信息,JLabel.CENTER);feedBack.setBackground(Color.green);inputNumber=newJTextField(0,5);buttonEnter=newJButton(确定);buttonEnter.addActionListener(this);buttonGetNumber.addActionListener(this);buttonGetNumber.addActionListener(this);BoxboxH1=Box.createHorizontalBox();boxH1.add(newJLabel(获取1-100之间的随机数:));boxH1.add(buttonGetNumber);BoxboxH2=Box.createHorizontalBox();boxH2.add(newJLabel(输入您的猜测:));boxH2.add(inputNumber);BoxboxH3=Box.createHorizontalBox();boxH3.add(newJLabel(单击确定按钮:));boxH3.add(buttonEnter);BoxboxH4=Box.createHorizontalBox();boxH4.add(newJLabel(反馈信息:));boxH4.add(feedBack);BoxbaseBox=Box.createVerticalBox();baseBox.add(boxH1);baseBox.add(boxH2);baseBox.add(boxH3);baseBox.add(boxH4);Containercon=getContentPane();con.setLayout(newFlowLayout());con.add(baseBox);con.validate();setBounds(120,125,279,200);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,150,150);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==buttonGetNumber){number=(int)(Math.random()*100)+1;}elseif(e.getSource()==buttonEnter){intguess=0;try{guess=Integer.parseInt(inputNumber.getText());if(guess==number){JOptionPane.showMessageDialog(this,猜对了);}elseif(guessnumber){JOptionPane.showMessageDialog(this,猜大了);inputNumber.setText(null);}elseif(guessnumber){JOptionPane.showMessageDialog(this,猜小了);inputNumber.setText(null);}}catch(NumberFormatExceptionevent){JOptionPane.showMessageDialog(this,请输入数字字符);}}}}publicclass猜数字游戏{publicstaticvoidmain(Stringargs[]){newWindowButton(猜数字小游戏);}}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功