GUI数字游戏
来源:互联网 发布:淘宝动漫店 编辑:程序博客网 时间:2024/05/17 03:16
1
3 import java.awt.Container; 4 import java.awt.GridLayout; 5 import java.awt.Toolkit; 6 7 import javax.swing.JFrame; 8 9 public class HiLoFrame extends JFrame {10 11 private Container contentP;12 13 private UpPanel upP;14 15 private MidPanel mdP;16 17 private DownPanel dnP;18 19 private int randomNum;20 21 public HiLoFrame() {22 this.randomNum = (int)(Math.random() * 50 + 50);23 System.out.println(this.randomNum);24 // 工具箱类--ToolKit25 Toolkit tk = Toolkit.getDefaultToolkit();26 // 设置窗体大小27 this.setSize(320, 290);28 // 设置窗体位置29 int screenW = (int) tk.getScreenSize().getWidth();// 得到屏幕宽30 int screenH = (int) tk.getScreenSize().getHeight();// 得到屏幕高31 this.setLocation((screenW - 320) / 2, (screenH - 290) / 2);32 // 设置窗体大小不可更改33 this.setResizable(true);34 // 设置窗体图标35 this.setIconImage(tk.createImage("img/hp.JPG"));36 // 设置窗体的标题37 this.setTitle("HiLo游戏");38 // 设置窗体关闭即为关闭程序39 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);40 // 设置窗体内容面板上所有的东西41 this.addContent();42 // 设置窗体可见43 this.setVisible(true);44 }45 46 47 48 private void addContent() {49 // TODO Auto-generated method stub50 this.contentP = this.getContentPane();51 this.contentP.setLayout(new GridLayout(3, 1));52 53 this.upP = new UpPanel();54 this.mdP = new MidPanel();55 this.dnP = new DownPanel(this);56 57 this.contentP.add(this.upP);58 this.contentP.add(this.mdP);59 this.contentP.add(this.dnP);60 }61 62 public UpPanel getUpP() {63 return upP;64 }65 66 public MidPanel getMdP() {67 return mdP;68 }69 70 public DownPanel getDnP() {71 return dnP;72 }73 74 public int getRandomNum() {75 return randomNum;76 }77 78 public void setRandomNum(int randomNum) {79 this.randomNum = randomNum;80 }81 82 }内部的功能
2 3 import java.awt.Color; 4 5 import javax.swing.BorderFactory; 6 import javax.swing.JPanel; 7 import javax.swing.JTextField; 8 9 //JPanel的默认布局管理是流布局10 public class UpPanel extends JPanel{11 12 private JTextField inputTxt;13 14 public UpPanel(){15 this.setBackground(Color.WHITE);16 this.setBorder(BorderFactory.createTitledBorder("Your Guess"));17 18 this.setLayout(null);19 this.inputTxt = new JTextField();20 this.inputTxt.setBounds(90, 33, 140,25);21 this.add(this.inputTxt);22 }23 24 public JTextField getInputTxt() {25 return inputTxt;26 }27 28 public void setInputTxt(JTextField inputTxt) {29 this.inputTxt = inputTxt;30 }31 }
2 3 import java.awt.Color; 4 import java.awt.FlowLayout; 5 import java.awt.Font; 6 7 import javax.swing.BorderFactory; 8 import javax.swing.JLabel; 9 import javax.swing.JPanel;10 11 public class MidPanel extends JPanel {12 13 private JLabel msgLab;14 15 public MidPanel(){16 this.setBackground(Color.WHITE);17 this.setLayout(new FlowLayout());18 this.setBorder(BorderFactory.createTitledBorder("Hint"));19 20 this.msgLab = new JLabel("Let's Play HiLo");21 this.msgLab.setFont(new Font("微软雅黑",Font.PLAIN,15));22 this.add(this.msgLab);23 }24 25 public JLabel getMsgLab() {26 return msgLab;27 }28 29 public void setMsgLab(JLabel msgLab) {30 this.msgLab = msgLab;31 }32 33 }
1 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 6 import javax.swing.JPanel; 7 8 public class DownPanel extends JPanel { 9 10 private ButtonPanel btnP;11 12 public DownPanel(HiLoFrame frame){13 this.setBackground(Color.WHITE);14 this.setLayout(new BorderLayout());15 16 this.btnP = new ButtonPanel(frame);17 this.add(this.btnP,BorderLayout.SOUTH);18 }19 20 public ButtonPanel getBtnP() {21 return btnP;22 }23 24 public void setBtnP(ButtonPanel btnP) {25 this.btnP = btnP;26 }27 28 }
1 2 3 import java.awt.Button; 4 import java.awt.Color; 5 import java.awt.FlowLayout; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 9 import javax.swing.JButton;10 import javax.swing.JLabel;11 import javax.swing.JPanel;12 13 public class ButtonPanel extends JPanel {14 15 private JButton enterBtn;16 17 private JButton replayBtn;18 19 private int i;20 21 private HiLoFrame frame;22 23 //匿名内部类中不能访问外部类的局部变量,只能访问全局变量或常量24 //这里的形参是一个欺骗编译器的动作25 public ButtonPanel( HiLoFrame frame){26 this.frame=frame;27 this.setBackground(Color.WHITE);28 this.setLayout(new FlowLayout());29 30 this.enterBtn = new JButton("确定");31 this.replayBtn = new JButton("重玩");32 this.replayBtn.setEnabled(false);33 this.enterBtn.setEnabled(true);34 this.enterBtn.addActionListener(new ActionListener() { 35 @Override36 public void actionPerformed(ActionEvent e) {37 // TODO Auto-generated method stub38 //1、取得输入的整数39 String inputStr = ButtonPanel.this.frame.getUpP().getInputTxt().getText();40 JLabel msgLab =ButtonPanel.this. frame.getMdP().getMsgLab();41 //2、取得随机数42 int randomNum =ButtonPanel.this. frame.getRandomNum();43 String msg = "";44 if(inputStr.matches("[5-9][0-9]")){45 int inputNum = Integer.parseInt(inputStr);46 //3、比较 47 if(inputNum > randomNum){48 msg = "不好意思,你猜大了!";49 msgLab.setText(msg+"你还有"+(6-i)+"次机会");50 }else if(inputNum < randomNum){51 msg = "不好意思,你猜小了!";52 msgLab.setText(msg+"你还有"+(6-i)+"次机会");53 }else{54 msg = "恭喜你猜对了!";55 msgLab.setText(msg);56 ButtonPanel.this.replayBtn.setEnabled(true);57 ButtonPanel.this.enterBtn.setEnabled(false);58 i=0;59 }60 61 }else{62 msg = "请输入50到99的有效数字!";63 msgLab.setText(msg+"你还有"+(7-i)+"次机会");64 }65 i++;66 67 if(i==7){68 ButtonPanel.this.replayBtn.setEnabled(true);69 ButtonPanel.this.enterBtn.setEnabled(false);70 i=0;71 }72 }73 });74 75 this.replayBtn.addActionListener(new ActionListener(){76 77 @Override78 public void actionPerformed(ActionEvent e) {79 // TODO Auto-generated method stub80 ButtonPanel.this.frame.setRandomNum((int)(Math.random() * 50 + 50)); 81 ButtonPanel.this.replayBtn.setEnabled(false);82 ButtonPanel.this.enterBtn.setEnabled(true);83 i=0;84 System.out.println(ButtonPanel.this.frame.getRandomNum());85 }86 87 });88 this.add(this.enterBtn);89 this.add(this.replayBtn);90 91 }92 93 }
12 13 public class TestMain {14 15 public static void main(String[] args) {16 // TODO Auto-generated method stub17 18 new HiLoFrame();19 20 }21 22 }
阅读全文
0 0
- GUI数字游戏
- 尝试为猜数字游戏写的GUI程序
- Java用GUI实现猜数字的游戏
- 游戏GUI
- Python基础语法(4)——GUI编程及猜数字游戏
- 游戏菜单GUI封装
- gui 打字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 数字游戏
- 文件复制系统调用版
- HTTP协议
- 第三方yum仓库搭建
- 数据结构——链表
- NYOJ 70: 阶乘因式分解(二)
- GUI数字游戏
- 使用webpack.DllPlugin与webpack.DllReferencePlugin解决webpack打包慢问题
- TCP服务器、客户端的简单实现
- AOP切点的合并
- 问题 O: 选美比赛
- 数据结构——队列
- 用Java代码读取Ecxel文件,再存入mysql数据库
- 【软工学习】第十二章——数据流图技术
- python内建函数type()