Java学习笔记04 俄罗斯方块的界面切换
来源:互联网 发布:控制器解析json数据 编辑:程序博客网 时间:2024/06/12 00:44
GameJFrame.java
package cn.edu.hytc;
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameJFrame extends JFrame {
public JPanel mainPanel;//主面板
public CardLayout cardLayout;//主面板的布局方式
//三个子面板
public IndexJPanel idexJPanel;//首页面板对象
public selectJPanel selectJPanel;//选择面板对象
public HelpJPanel helpJPanel;//选择面板对象
public GameJFrame()
{
//创建主面板及布局
mainPanel=new JPanel();
cardLayout=new CardLayout();
mainPanel.setLayout(cardLayout);//使用布局
//创建三个子面板
idexJPanel=new IndexJPanel(mainPanel,cardLayout);
selectJPanel=new selectJPanel(mainPanel,cardLayout);
helpJPanel=new HelpJPanel(mainPanel,cardLayout);
//将三个子面板添加到主面板上
mainPanel.add(idexJPanel,"idexJPanel");
mainPanel.add(selectJPanel,"selectJPanel");
mainPanel.add(helpJPanel,"helpJPanel");
JPanel c=(JPanel)this.getContentPane();
c.add(mainPanel);
this.setTitle("俄罗斯方块");
this.setSize(330, 440);//设置窗口的宽高
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);//屏幕居中
this.setResizable(false);//不允许改动
this.setVisible(true);
}
public static void main(String arge[])
{
new GameJFrame();
}
}
package cn.edu.hytc;
import java.awt.CardLayout;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.w3c.dom.events.MouseEvent;
//游戏的首页面板
public class IndexJPanel extends JPanel implements MouseListener{
private JButton start_jbtn;//开始游戏的按钮
private JButton help_jbtn;//新手指导按钮
private JButton exit_jbtn;//退出按钮
//tupian
private ImageIcon start_imageIcon;//开始游戏的按钮图片
private ImageIcon help_imageIcon;//新手指导按钮图片
private ImageIcon exit_imageIcon;//退出按钮图片
private ImageIcon start_imgIcon_b,help_imgIcon_b,exit_imgIcon_b;//变大后的背景图标
private int x=90;int y=40;//按钮的大小
private int vd=60;//按钮垂直间距
public JPanel mainPanel;//主面板
public CardLayout cardLayout;//主面板的布局方式
public IndexJPanel(JPanel m,CardLayout c)
{
this.mainPanel=m;
this.cardLayout=c;
//采用绝对布局方式
this.setLayout(null);//参数为空则无布局
//初始图片按钮的设置
start_imageIcon=Tool.getIcon("src//images//game_start.png");
start_jbtn=new JButton(start_imageIcon);
start_jbtn.setBounds(x, y,start_imageIcon.getIconWidth(),start_imageIcon.getIconHeight());
this.add(start_jbtn);
help_imageIcon=Tool.getIcon("src//images//game_help.png");
help_jbtn=new JButton(help_imageIcon);
help_jbtn.setBounds(x,y+vd+help_imageIcon.getIconHeight(),help_imageIcon.getIconWidth(),help_imageIcon.getIconHeight());
this.add(help_jbtn);
exit_imageIcon=Tool.getIcon("src//images//exit.png");
exit_jbtn=new JButton(exit_imageIcon);
exit_jbtn.setBounds(x,y+vd+vd+2*exit_imageIcon.getIconHeight(),exit_imageIcon.getIconWidth(),exit_imageIcon.getIconHeight());
this.add(exit_jbtn);
//获取变大后的图片
start_imgIcon_b=Tool.getIcon("src//images//game_start_b.png");
start_jbtn.addMouseListener(this);
help_imgIcon_b=Tool.getIcon("src//images//game_help_b.png");
help_jbtn.addMouseListener(this);
exit_imgIcon_b=Tool.getIcon("src//images//exit_b.png");
exit_jbtn.addMouseListener(this);
}
@Override//鼠标单击
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==start_jbtn){
this.cardLayout.show(this.mainPanel, "selectJPanel");//点击开始按钮,转向选择界面
}
else if(e.getSource()==help_jbtn)
{
this.cardLayout.show(this.mainPanel, "helpJPanel");//点击帮助按钮,转向新手指导界面
}
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override//鼠标进入组件
public void mouseEntered(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
//设置图片变大
//判断组件
if(e.getSource()==start_jbtn)
{
start_jbtn.setBounds(x-(start_imgIcon_b.getIconWidth()-start_imageIcon.getIconWidth())/2,y-(start_imgIcon_b.getIconHeight()-start_imageIcon.getIconHeight())/2,start_imgIcon_b.getIconWidth(),start_imgIcon_b.getIconHeight());
start_jbtn.setIcon(start_imgIcon_b);
}
else if(e.getSource()==help_jbtn)
{
help_jbtn.setBounds(x-(help_imgIcon_b.getIconWidth()-help_imageIcon.getIconWidth())/2,y-(help_imgIcon_b.getIconHeight()-help_imageIcon.getIconHeight())/2+vd+help_jbtn.getHeight(),help_imgIcon_b.getIconWidth(),help_imgIcon_b.getIconHeight());
help_jbtn.setIcon(help_imgIcon_b);
}
else if(e.getSource()==exit_jbtn)
{
exit_jbtn.setBounds(x-(exit_imgIcon_b.getIconWidth()-exit_imageIcon.getIconWidth())/2,y-(exit_imgIcon_b.getIconHeight()-exit_imageIcon.getIconHeight())/2+2*vd+2*exit_jbtn.getHeight(),exit_imgIcon_b.getIconWidth(),exit_imgIcon_b.getIconHeight());
exit_jbtn.setIcon(exit_imgIcon_b);
}
}
@Override//鼠标退出组件
public void mouseExited(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
//设置图片变回原来的大小
//判断组件
if(e.getSource()==start_jbtn)
{
start_jbtn.setBounds(x, y,start_imageIcon.getIconWidth(),start_imageIcon.getIconHeight());
start_jbtn.setIcon(start_imageIcon);
}
else if(e.getSource()==help_jbtn)
{
help_jbtn.setBounds(x,y+vd+help_imageIcon.getIconHeight(),help_imageIcon.getIconWidth(),help_imageIcon.getIconHeight());
help_jbtn.setIcon(help_imageIcon);
}
else if(e.getSource()==exit_jbtn)
{
exit_jbtn.setBounds(x,y+vd+vd+2*exit_imageIcon.getIconHeight(),exit_imageIcon.getIconWidth(),exit_imageIcon.getIconHeight());
exit_jbtn.setIcon(exit_imageIcon);
}
}
}
package cn.edu.hytc;
import java.awt.CardLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class selectJPanel extends JPanel implements MouseListener{
private JButton easy_jbtn;//声明新手按钮
private JButton normal_jbtn;//声高级指导按钮
private JButton hard_jbtn;//声明大师游戏按钮
private JButton hell_jbtn;//声明地狱指导按钮
private JButton back_jbtn;//声明返回游戏按钮
private ImageIcon easy_imageIcon;//声明新手按钮默认图标
private ImageIcon normal_imageIcon;//声明新手按钮默认图标
private ImageIcon hard_imageIcon;//声明新手按钮默认图标
private ImageIcon hell_imageIcon;//声明新手按钮默认图标
private ImageIcon back_imageIcon;//声明新手按钮默认图标
private ImageIcon easy_imageIcon_b;//变大后的背景图标
private ImageIcon normal_imageIcon_b;
private ImageIcon hard_imageIcon_b;
private ImageIcon hell_imageIcon_b;
private ImageIcon back_imageIcon_b;
private int x=90,y=20,vd=25;
public JPanel mainPanel;//主面板
public CardLayout cardLayout;//主面板的布局方式
public selectJPanel(JPanel m,CardLayout c)
{
this.mainPanel=m;
this.cardLayout=c;
this.setLayout(null);
easy_imageIcon=Tool.getIcon("src//images//easy.png");
easy_jbtn=new JButton(easy_imageIcon);
easy_jbtn.setBounds(x, y,easy_imageIcon.getIconWidth(),easy_imageIcon.getIconHeight());
this.add(easy_jbtn);
normal_imageIcon=Tool.getIcon("src//images//normal.png");
normal_jbtn=new JButton(normal_imageIcon);
normal_jbtn.setBounds(x,y+vd+normal_imageIcon.getIconHeight(),normal_imageIcon.getIconWidth(),normal_imageIcon.getIconHeight());
this.add(normal_jbtn);
hard_imageIcon=Tool.getIcon("src//images//hard.png");
hard_jbtn=new JButton(hard_imageIcon);
hard_jbtn.setBounds(x,y+vd+vd+2*hard_imageIcon.getIconHeight(),hard_imageIcon.getIconWidth(),hard_imageIcon.getIconHeight());
this.add(hard_jbtn);
hell_imageIcon=Tool.getIcon("src//images//hell.png");
hell_jbtn=new JButton(hell_imageIcon);
hell_jbtn.setBounds(x,y+3*vd+3*hell_imageIcon.getIconHeight(),hell_imageIcon.getIconWidth(),hell_imageIcon.getIconHeight());
this.add(hell_jbtn);
back_imageIcon=Tool.getIcon("src//images//back.png");
back_jbtn=new JButton(back_imageIcon);
back_jbtn.setBounds(x,y+4*vd+4*back_imageIcon.getIconHeight(),back_imageIcon.getIconWidth(),back_imageIcon.getIconHeight());
this.add(back_jbtn);
//获取变大后的图片
easy_imageIcon_b=Tool.getIcon("src//images//easy_b.png");
easy_jbtn.addMouseListener(this);
normal_imageIcon_b=Tool.getIcon("src//images//normal_b.png");
normal_jbtn.addMouseListener(this);
hard_imageIcon_b=Tool.getIcon("src//images//hard_b.png");
hard_jbtn.addMouseListener(this);
hell_imageIcon_b=Tool.getIcon("src//images//hell_b.png");
hell_jbtn.addMouseListener(this);
back_imageIcon_b=Tool.getIcon("src//images//back_b.png");
back_jbtn.addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==back_jbtn)
{
this.cardLayout.show(this.mainPanel, "idexJPanel");//点击返回按钮,转向开始界面
}
}
@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) {
// TODO Auto-generated method stub
if(e.getSource()==easy_jbtn)
{
easy_jbtn.setBounds(x-(easy_imageIcon_b.getIconWidth()-easy_imageIcon.getIconWidth())/2,y-(easy_imageIcon_b.getIconHeight()-easy_imageIcon.getIconHeight())/2,easy_imageIcon_b.getIconWidth(),easy_imageIcon_b.getIconHeight());
easy_jbtn.setIcon(easy_imageIcon_b);
}
else if(e.getSource()==normal_jbtn)
{
normal_jbtn.setBounds(x-(normal_imageIcon_b.getIconWidth()-normal_imageIcon.getIconWidth())/2,y-(normal_imageIcon_b.getIconHeight()-normal_imageIcon.getIconHeight())/2+vd+normal_jbtn.getHeight(),normal_imageIcon_b.getIconWidth(),normal_imageIcon_b.getIconHeight());
normal_jbtn.setIcon(normal_imageIcon_b);
}
else if(e.getSource()==hard_jbtn)
{
hard_jbtn.setBounds(x-(hard_imageIcon_b.getIconWidth()-hard_imageIcon.getIconWidth())/2,y-(hard_imageIcon_b.getIconHeight()-hard_imageIcon.getIconHeight())/2+2*vd+2*hard_jbtn.getHeight(),hard_imageIcon_b.getIconWidth(),hard_imageIcon_b.getIconHeight());
hard_jbtn.setIcon(hard_imageIcon_b);
}
else if(e.getSource()==hell_jbtn)
{
hell_jbtn.setBounds(x-(hell_imageIcon_b.getIconWidth()-hell_imageIcon.getIconWidth())/2,y-(hell_imageIcon_b.getIconHeight()-hell_imageIcon.getIconHeight())/2+3*vd+3*hell_jbtn.getHeight(),hell_imageIcon_b.getIconWidth(),hell_imageIcon_b.getIconHeight());
hell_jbtn.setIcon(hell_imageIcon_b);
}
else if(e.getSource()==back_jbtn)
{
back_jbtn.setBounds(x-(back_imageIcon_b.getIconWidth()-back_imageIcon.getIconWidth())/2,y-(back_imageIcon_b.getIconHeight()-back_imageIcon.getIconHeight())/2+4*vd+4*back_jbtn.getHeight(),back_imageIcon_b.getIconWidth(),back_imageIcon_b.getIconHeight());
back_jbtn.setIcon(back_imageIcon_b);
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==easy_jbtn)
{
easy_jbtn.setBounds(x, y,easy_imageIcon.getIconWidth(),easy_imageIcon.getIconHeight());
easy_jbtn.setIcon(easy_imageIcon);
}
else if(e.getSource()==normal_jbtn)
{
normal_jbtn.setBounds(x,y+vd+normal_imageIcon.getIconHeight(),normal_imageIcon.getIconWidth(),normal_imageIcon.getIconHeight());
normal_jbtn.setIcon(normal_imageIcon);
}
else if(e.getSource()==hard_jbtn)
{
hard_jbtn.setBounds(x,y+vd+vd+2*hard_imageIcon.getIconHeight(),hard_imageIcon.getIconWidth(),hard_imageIcon.getIconHeight());
hard_jbtn.setIcon(hard_imageIcon);
}
else if(e.getSource()==hell_jbtn)
{
hell_jbtn.setBounds(x,y+3*vd+3*hell_imageIcon.getIconHeight(),hell_imageIcon.getIconWidth(),hell_imageIcon.getIconHeight());
hell_jbtn.setIcon(hell_imageIcon);
}
else if(e.getSource()==back_jbtn)
{
back_jbtn.setBounds(x,y+4*vd+4*back_imageIcon.getIconHeight(),back_imageIcon.getIconWidth(),back_imageIcon.getIconHeight());
back_jbtn.setIcon(back_imageIcon);
}
}
}
package cn.edu.hytc;
import java.awt.CardLayout;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.w3c.dom.events.MouseEvent;
public class HelpJPanel extends JPanel implements MouseListener {
private JLabel operJLabel,copyJLabel;
private JButton back_jbtn;
int x=70,y=20;
public JPanel mainPanel;//主面板
public CardLayout cardLayout;//主面板的布局方式
private ImageIcon back_imgIcon,back_imgIcon_b;
public HelpJPanel(JPanel m,CardLayout c)
{
this.mainPanel=m;
this.cardLayout=c;
this.setLayout(null);
operJLabel=new JLabel("<html>操作<br/>按向上键 方块变形<br/>按向下键 方块加速下落<br/>按向左键 方块向左<br/>按向右键 方块向右</html>");
operJLabel.setBounds(x , y, 200, 100);
copyJLabel=new JLabel("<html>淮阴师范学院<br/>邮箱<a href=''>123@qq.com</a><br/>版权所有 违者必究</html>");
copyJLabel.setBounds(x, y+100+20, 200, 100);
this.add(operJLabel);
this.add(copyJLabel);
back_imgIcon=Tool.getIcon("src//images//back.png");
back_imgIcon_b=Tool.getIcon("src//images//back_b.png");
back_jbtn=new JButton(back_imgIcon);
back_jbtn.setBounds(x+20, y+150+100, back_imgIcon.getIconWidth(), back_imgIcon.getIconHeight());
this.add(back_jbtn);
back_jbtn.addMouseListener(this);
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==back_jbtn)
{
this.cardLayout.show(this.mainPanel, "idexJPanel");//点击返回按钮,转向开始界面
}
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
back_jbtn.setBounds(x+20-(back_imgIcon_b.getIconWidth()-back_imgIcon.getIconWidth())/2, y+150+100-(back_imgIcon_b.getIconHeight()-back_imgIcon.getIconHeight())/2, back_imgIcon_b.getIconWidth(), back_imgIcon_b.getIconHeight());
back_jbtn.setIcon(back_imgIcon_b);
; }
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
back_jbtn.setBounds(x+20, y+150+100, back_imgIcon.getIconWidth(), back_imgIcon.getIconHeight());
back_jbtn.setIcon(back_imgIcon);
}
// public static void main(String args[])
// {
// JFrame f=new JFrame();
// HelpJPanel help=new HelpJPanel();
// f.add(help);
// f.setBounds(100,100, 300, 440);
// f.setVisible(true);
// }
}
package cn.edu.hytc;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
public class Tool {
//获取图片
public static ImageIcon getIcon(String filename)
{
//获取图片对象
Image image=Toolkit.getDefaultToolkit().getImage(filename);
ImageIcon imageIcon=new ImageIcon(image);
return imageIcon;
}
}
- Java学习笔记04 俄罗斯方块的界面切换
- Java学习笔记03 俄罗斯方块界面
- Android学习笔记2---多界面切换
- 【linux系统学习笔记】Ubuntu文本界面和图像界面的切换
- 一个字符界面的俄罗斯方块
- JAVA版俄罗斯方块学习
- java开发俄罗斯方块学习笔记 Day-1 分析
- java开发俄罗斯方块学习笔记 Day-2 设计
- java开发俄罗斯方块学习笔记 Day-3 素材
- java开发俄罗斯方块学习笔记 Day-4 显示
- java开发俄罗斯方块学习笔记 Day-5 封装
- java开发俄罗斯方块学习笔记 Day-6 布局
- java开发俄罗斯方块学习笔记 Day-7 多态
- java开发俄罗斯方块学习笔记 Day-8 配置
- 俄罗斯方块的学习
- Java俄罗斯方块源代码(swing界面)
- android 开发零起步学习笔记(十一):界面切换+几种常用界面切换效果
- JAVA写的俄罗斯方块
- 【基础练习】【数论/模拟】codevs1670 无穷的序列题解
- 最大公约数Greatest Common Divisor
- TestNG - Logger类的使用
- DOS命令集锦——让人见文知意
- java实现串口配置之运行环境搭建
- Java学习笔记04 俄罗斯方块的界面切换
- 笔试题 百度2015大数据云计算研发笔试(非相邻数最大和)
- 转x电容和y电容的区别
- 后台偷发短信
- Codevs1992题解
- test
- Eclipse for Mac 配置JDK1.8
- 从vivado 2013.4到vivado 2015.2的搬迁笔记
- 人群拥挤检测之人数检测二