java 猜数字小游戏
来源:互联网 发布:渭南网络电视台 编辑:程序博客网 时间:2024/04/30 01:19
GuessNum:
package 猜数字1;public class GuessNum {public static void main(String args[]){BuildWindow win=new BuildWindow("猜数字");}}
BuildWindow:
package 猜数字1;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class BuildWindow extends JFrame{static JButton guessButton;//为了能够在PoliceListener内e.getSource调用它static JButton enterButton;//百度好久才找到static JLabel result;//JTextField showText;PoliceListener listener;public BuildWindow(String s){init();setVisible(true);setBounds(100,100,260,160);setTitle(s);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}void init(){setLayout(new FlowLayout());guessButton = new JButton("获取随机数");add(guessButton);result = new JLabel("lala");add(result);result.setBackground(Color.blue);showText = new JTextField(10);add(showText);enterButton = new JButton("确定");add(enterButton);listener=new PoliceListener();//第一次忘记了。。结果不出现结果。找了好久guessButton.addActionListener(listener);enterButton.addActionListener(listener);listener.setJTextField(showText);}}PoliceListener:
package 猜数字1;import javax.swing.*;import java.awt.event.*;public class PoliceListener implements ActionListener{int num;JTextField showText;public void setJTextField(JTextField text){showText=text;}public void actionPerformed (ActionEvent e){if(e.getSource()==BuildWindow.guessButton){num=(int)(Math.random()*100)+1;BuildWindow.result.setText("输入你的猜测(1-100)");}else if(e.getSource()==BuildWindow.enterButton){int guess;try{guess=Integer.parseInt(showText.getText());if(guess==num){BuildWindow.result.setText("猜对了!");}else if(guess>num){BuildWindow.result.setText("猜大了");}else if(guess<num){BuildWindow.result.setText("猜小了");}}catch(NumberFormatException event){BuildWindow.result.setText("请输入数字字符");}}}}
这是运行结果不知道这叫不叫可视化。。哈哈胡说的 莫怪
1 0
- 【java】猜数字小游戏
- java 猜数字小游戏
- Java 猜数字小游戏
- JAVA猜数字小游戏
- java猜数字组合小游戏
- java applet 猜数字小游戏
- 【JAVA学习】猜数字小游戏
- java-猜数字小游戏案例
- 【Java学习】Java命令行猜数字小游戏
- 第一个JAVA程序--猜数字小游戏
- Java重拾之猜数字小游戏
- [Java小案例01] 猜数字小游戏
- JAVA学习笔记:猜数字小游戏
- 使用Java写一个猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏(MiniGUI)
- 猜数字小游戏
- 服务提供者框架(Service Provider Framework)
- ffmpeg配置
- “米粉节”背后的故事——小米网抢购系统开发实践
- android:src
- CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
- java 猜数字小游戏
- POJ 2686 Traveling by Stagecoach
- poj2594:Treasure Exploration
- c++迭代器详解(一):back_inserter, front_inserter,inserter
- 单链表(七)——判断两链表是否交叉
- 斯坦福Developing iOS 8 Apps学习笔记(四)
- linux 安装flash
- 《剑指offer》求1+2+3+...+n
- Eclipse关联java源代码