龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

Java基于Swing实现的打猎射击游戏代码(6)

时间:2014-11-14 11:09来源:网络整理 作者:网络 点击:
分享到:
setAlwaysOnTop(true);// 是窗体保持在最顶层 setBounds(100, 100, 573, 411); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); backgroundPanel = new BackgroundPanel();// 创建带背景的面板 b

                setAlwaysOnTop(true);// 是窗体保持在最顶层
                setBounds(100, 100, 573, 411);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                backgroundPanel = new BackgroundPanel();// 创建带背景的面板
                backgroundPanel.setImage(new ImageIcon(getClass().getResource(
                                "background.jpg")).getImage());// 设置背景图片
                getContentPane().add(backgroundPanel, BorderLayout.CENTER);
                // 添加鼠标事件适配器
                addMouseListener(new FrameMouseListener());
                scoreLabel = new JLabel();// 显示分数的标签组件
                scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
                scoreLabel.setForeground(Color.ORANGE);
                scoreLabel.setText("分数:");
                scoreLabel.setBounds(25, 15, 120, 18);
                backgroundPanel.add(scoreLabel);
                ammoLabel = new JLabel();// 显示自动数量的标签组件
                ammoLabel.setForeground(Color.ORANGE);
                ammoLabel.setHorizontalAlignment(SwingConstants.RIGHT);
                ammoLabel.setText("子弹数量:" + ammoNum);
                ammoLabel.setBounds(422, 15, 93, 18);
                backgroundPanel.add(ammoLabel);
        }

        /**
         * 加分方法
         */
        public synchronized static void appScore(int num) {
                score += num;
                scoreLabel.setText("分数:" + score);
        }

        /**
         * 消耗子弹的方法
         */
        public synchronized static void useAmmo() {
                synchronized (ammoNum) {
                        ammoNum--;// 子弹数量递减
                        ammoLabel.setText("子弹数量:" + ammoNum);
                        if (ammoNum <= 0) {// 判断子弹是否小于0
                                new Thread(new Runnable() {
                                        public void run() {
                                                // 显示提示信息面板
                                                infoPane.setVisible(true);
                                                try {
                                                        // 1秒钟装载子弹的时间
                                                        Thread.sleep(1000);
                                                } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                }
                                                ammoNum = 5;// 恢复子弹数量
                                                // 修改子弹数量标签的文本
                                                ammoLabel.setText("子弹数量:" + ammoNum);
                                                infoPane.setVisible(false);// 隐藏提示信息面板
                                        }
                                }).start();
                        }
                }
        }

精彩图集

赞助商链接