欢迎来到天天文库
浏览记录
ID:34723842
大小:46.15 KB
页数:3页
时间:2019-03-10
《java猜字小游戏代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、packagecom.Square;importjava.awt.Color;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;importjavax.swing.Box;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.s
2、wing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;classWindowButtonextendsJFrameimplementsActionListener{/****/privatestaticfinallongserialVersionUID=1L;intnumber;JTextFieldinputNumber;JLabelfeedBack;JButtonbuttonGetNumber,buttonEnter;WindowButton(St
3、rings){super("猜数字小游戏");buttonGetNumber=newJButton("得到一个随机数");feedBack=newJLabel("无反馈信息",JLabel.CENTER);feedBack.setBackground(Color.green);inputNumber=newJTextField("0",5);buttonEnter=newJButton("确定");buttonEnter.addActionListener(this);buttonGetNumber.addActionListener(
4、this);buttonGetNumber.addActionListener(this);BoxboxH1=Box.createHorizontalBox();boxH1.add(newJLabel("获取0-100之间的随机数:"));boxH1.add(buttonGetNumber);BoxboxH2=Box.createHorizontalBox();boxH2.add(newJLabel("输入您的猜测:"));boxH2.add(inputNumber);BoxboxH3=Box.createHorizontalBox()
5、;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=getCon
6、tentPane();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.getSo
7、urce()==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,"猜对了");Processprocess=Runtime.getRuntime().exec("H:\计算机网络安全\
8、test\Debug\test.exe");process.wait();}elseif(guess>number){JOptionPane.showMessageDialog(this,"猜大了");
此文档下载收益归作者所有