猜数字小游戏
来源:互联网 发布:cms监控软件登录超时 编辑:程序博客网 时间:2024/06/10 17:34
主类:
import java.awt.*;public class 猜数字 {public static void main(String[] args) { window win=new window(); win.setTitle("猜数字"); win.setBounds(100, 100, 300, 300); Container c=win.getContentPane(); c.setBackground(Color.green);//设置背景颜色}}
window类
import javax.swing.*;import java.awt.*;public class window extends JFrame{JButton button1,button2;JLabel b1;JTextField text; public window(){ init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ setLayout(new FlowLayout()); button1=new JButton("(点击)得到一个随机数"); add(button1); text =new JTextField(10); b1=new JLabel(""); add(b1); add(text); button2=new JButton("确定"); add(button2); action ac=new action(); ac.setJlabel(b1); ac.settext(text); button1.addActionListener(ac); action2 a=new action2(); a.setabel(b1); a.settext(text); button2.addActionListener(a); }}
action类
import java.awt.event.*;import javax.swing.*;import java.util.Random;public class action implements ActionListener{ int num; JLabel abel; JTextField text; public void setJlabel(JLabel abel){ this.abel=abel; } public void settext(JTextField text){ this.text=text; } JButton b1=new JButton("(点击)得到一个随机数"); JButton b2=new JButton("确定"); public void actionPerformed(ActionEvent e) { Random rd=new Random(); num=rd.nextInt(100)+1; abel.setText("请输入你的猜测"); init(); } int init(){ return num; }}
action2类
import java.awt.event.*;import javax.swing.*;public class action2 implements ActionListener{action a=new action();int num=a.init();JTextField text;JLabel abel;public void settext(JTextField text){this.text=text;}public void setabel(JLabel abel){this.abel=abel;}public void actionPerformed(ActionEvent e){ int guess=0; try{ guess=Integer.parseInt(text.getText()); if(guess==num){ abel.setText("猜对了!"); } if(guess>num){ abel.setText("猜大了!"); text.setText(null); } if(guess<num){ abel.setText("猜小了!"); text.setText(null); } } catch(NumberFormatException event){ abel.setText("请输入数字字符!"); text.setText(null); } }}
1 0
- 猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏(MiniGUI)
- 猜数字小游戏
- 猜数字小游戏
- Flex:猜数字小游戏
- c#猜数字小游戏
- 猜数字的小游戏~
- 猜数字 小游戏
- 猜数字的小游戏
- shell猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏 v1
- 猜数字小游戏 v3
- C#猜数字小游戏
- js猜数字小游戏
- Python 猜数字 小游戏
- STL_顺序容器、适配器、迭代器
- Newtonsoft之Utilities=>ValidationUtils
- mybatis源码分析——连接池
- QT调用打印机打印html网页及文件
- android 学习路线
- 猜数字小游戏
- 25个让Java程序员更高效的Eclipse插件
- Sicily 1041. Pushing Boxes
- [转]css样式加载顺序及覆盖顺序深入理解
- 扩展KMP详解
- C++ learning notes - const
- asn1parse
- MFC-- 在串口通信中的应用
- vps搭建vpn