项目实战之天天酷跑(二):开始游戏界面

您所在的位置:网站首页 跑酷游戏界面图片 项目实战之天天酷跑(二):开始游戏界面

项目实战之天天酷跑(二):开始游戏界面

2023-12-18 03:48| 来源: 网络整理| 查看: 265

项目实战之天天酷跑(二):开始游戏界面 转载

javaSpr℡¹⁷ 2021-05-25 10:20:14

文章标签 project 天天酷跑 文章分类 游戏开发

项目实战之天天酷跑(二):开始游戏界面_mjtabu的技术博客_51CTO博客 项目源码及相关图片资源,已上传至GitHub:https://github.com/HueyM/Projects

前文,我们完成了登录界面的搭建。本文将完成开始游戏界面的搭建,并建立起登录界面与开始游戏界面的桥梁。实现在输对用户名和密码后即可进入开始游戏界面的功能。

界面功能需求图:

项目实战之天天酷跑(二):开始游戏界面_天天酷跑

具体要求:

当鼠标移入开始游戏按钮后,按钮将由暗变亮,鼠标移开后,按钮又由亮变暗。

帮助、离开按钮同理。

另外,当点击离开时,需要实现关闭当前界面的效果。

上代码: 项目实战之天天酷跑(二):开始游戏界面_project_02项目实战之天天酷跑(二):开始游戏界面_project_03 package cn.sqc.runday.view; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import cn.sqc.runday.controller.WindowFrame; public class MainFrame extends JFrame implements MouseListener { //设置窗体的基本属性 大小 /** * 1.1、设置窗体基本属性大小 居中 边框隐藏 默认关闭按钮 logo图标 1.2、创建背景面板MainPanel,实现背景图片功能 2.图片按钮功能 */ //2.1创建开始按钮 帮助按钮 离开按钮 组件 JLabel start,help,exit; JPanel MainPanel; public MainFrame() {//无参构造,创建对象。并在main函数中调用 //2.2 start = new JLabel(new ImageIcon("Image/hh1.png"));//ImageIcon:图标 start.setBounds(350,320,150,40); start.setEnabled(false);//false按钮为灰色 start.addMouseListener(this); this.add(start); help = new JLabel(new ImageIcon("Image/hh2.png")); help.setBounds(350,420,150,40); help.setEnabled(false); help.addMouseListener(this); this.add(help); exit = new JLabel(new ImageIcon("Image/hh3.png")); exit.setBounds(350, 520, 150, 40); exit.setEnabled(false); exit.addMouseListener(this); this.add(exit); /**1.实现背景图片及窗体属性*/ MainPanel panel = new MainPanel(); this.add(panel); //设置窗体基本属性大小 居中 边框隐藏 默认关闭按钮 logo图标 this.setSize(1200,730);//大小 this.setLocationRelativeTo(null);//居中 this.setUndecorated(true);//边框隐藏 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭 this.setIconImage(new ImageIcon("Image/115.png").getImage());//logo this.setVisible(true); } public static void main(String[] args) { new MainFrame(); } //2、创建背景面板MainPanel,实现背景图片功能 class MainPanel extends JPanel{//创建的MainPanel类,在MainFrame中调用 Image background; public MainPanel() { try { background = ImageIO.read(new File("Image/main.png")); } catch (IOException e) { e.printStackTrace(); } } @Override public void paint(Graphics g) { super.paint(g); g.drawImage(background, 0, 0,1200,730, null); } } //以下五个方法均为添加 implements MouseListener 后,快捷出来的 @Override public void mouseClicked(MouseEvent e) { //鼠标点击 if(e.getSource().equals(start)){ //跳转到下一界面 new WindowFrame().Start(); //关闭当前界面 //dispose(); }else if(e.getSource().equals(exit)){ dispose(); }else if(e.getSource().equals(help)){ JOptionPane.showMessageDialog(null, "有疑问请联系开发者:Huey"); } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // 鼠标移入 if(e.getSource().equals(start)){//e指一个事件。e.getSource()获取事件 //如果鼠标移入到(start)组件(图片按钮) start.setEnabled(true); }else if(e.getSource().equals(help)){ help.setEnabled(true); }else if(e.getSource().equals(exit)){ exit.setEnabled(true); } } @Override public void mouseExited(MouseEvent e) { //鼠标移出 if(e.getSource().equals(start)){ start.setEnabled(false); }else if(e.getSource().equals(help)){ help.setEnabled(false); }else if(e.getSource().equals(exit)){ exit.setEnabled(false); } } } View Code

 

测试:

先填补上文的缺憾,加上new MainFrame();语句。调用我们刚刚写好的开始游戏界面。

项目实战之天天酷跑(二):开始游戏界面_天天酷跑_04登录界面:项目实战之天天酷跑(二):开始游戏界面_天天酷跑_05单击确定

项目实战之天天酷跑(二):开始游戏界面_天天酷跑_06

完美进入我们写好的登录游戏界面:项目实战之天天酷跑(二):开始游戏界面_天天酷跑_07现在看开始游戏按钮:项目实战之天天酷跑(二):开始游戏界面_天天酷跑_08帮助按钮:项目实战之天天酷跑(二):开始游戏界面_天天酷跑_09点击帮助按钮:

项目实战之天天酷跑(二):开始游戏界面_project_10

退出按钮:项目实战之天天酷跑(二):开始游戏界面_project_11点击:项目实战之天天酷跑(二):开始游戏界面_project_12大功告成!

            收藏 评论 分享 举报

上一篇:项目实战之天天酷跑(三):缓冲加载游戏界面

下一篇:项目实战之天天酷跑(一):登录界面



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3