<pre name="code" class="java"><span style="font-size:32px;">GameJFrame.java:</span>
<span style="font-size:18px;">package cn.edu.hytc;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameJFrame extends JFrame {
public IndexJPanel idexJPanel;//首页面板对象
public GameJFrame()
{
idexJPanel=new IndexJPanel();
JPanel c=(JPanel)this.getContentPane();
c.add(idexJPanel);
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();
}
}</span>
<span style="font-size:32px;">IndexJPanel.java:</span>
p<span style="font-size:18px;">ackage cn.edu.hytc;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.w3c.dom.events.MouseEvent;
<span style="white-space:pre"> </span>//游戏的首页面板
public class IndexJPanel extends JPanel implements MouseListener{
<span style="white-space:pre"> </span>private JButton start_jbtn;//开始游戏的按钮
<span style="white-space:pre"> </span>private JButton help_jbtn;//新手指导按钮
<span style="white-space:pre"> </span>private JButton exit_jbtn;//退出按钮
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//tupian
<span style="white-space:pre"> </span>private ImageIcon start_imageIcon;//开始游戏的按钮图片
<span style="white-space:pre"> </span>private ImageIcon help_imageIcon;//新手指导按钮图片
<span style="white-space:pre"> </span>private ImageIcon exit_imageIcon;//退出按钮图片
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>private ImageIcon start_imgIcon_b,help_imgIcon_b,exit_imgIcon_b;//变大后的背景图标
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>private int x=90;int y=40;//按钮的大小
<span style="white-space:pre"> </span>private int vd=60;//按钮垂直间距
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public IndexJPanel()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//采用绝对布局方式
<span style="white-space:pre"> </span>this.setLayout(null);//参数为空则无布局
<span style="white-space:pre"> </span>//初始图片按钮的设置
<span style="white-space:pre"> </span>start_imageIcon=Tool.getIcon("src//images//game_start.png");<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>start_jbtn=new JButton(start_imageIcon);
<span style="white-space:pre"> </span>start_jbtn.setBounds(x, y,start_imageIcon.getIconWidth(),start_imageIcon.getIconHeight());
<span style="white-space:pre"> </span>this.add(start_jbtn);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>help_imageIcon=Tool.getIcon("src//images//game_help.png");
<span style="white-space:pre"> </span>help_jbtn=new JButton(help_imageIcon);
<span style="white-space:pre"> </span>help_jbtn.setBounds(x,y+vd+help_imageIcon.getIconHeight(),help_imageIcon.getIconWidth(),help_imageIcon.getIconHeight());
<span style="white-space:pre"> </span>this.add(help_jbtn);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>exit_imageIcon=Tool.getIcon("src//images//exit.png");<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>exit_jbtn=new JButton(exit_imageIcon);
<span style="white-space:pre"> </span>exit_jbtn.setBounds(x,y+vd+vd+2*exit_imageIcon.getIconHeight(),exit_imageIcon.getIconWidth(),exit_imageIcon.getIconHeight());
<span style="white-space:pre"> </span>this.add(exit_jbtn);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//获取变大后的图片
<span style="white-space:pre"> </span>start_imgIcon_b=Tool.getIcon("src//images//game_start_b.png");
<span style="white-space:pre"> </span>start_jbtn.addMouseListener(this);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>help_imgIcon_b=Tool.getIcon("src//images//game_help_b.png");
<span style="white-space:pre"> </span>help_jbtn.addMouseListener(this);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>exit_imgIcon_b=Tool.getIcon("src//images//exit_b.png");
<span style="white-space:pre"> </span>exit_jbtn.addMouseListener(this);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseClicked(java.awt.event.MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mousePressed(java.awt.event.MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseReleased(java.awt.event.MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override//鼠标进入组件
<span style="white-space:pre"> </span>public void mouseEntered(java.awt.event.MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>//设置图片变大
<span style="white-space:pre"> </span>//判断组件
<span style="white-space:pre"> </span>if(e.getSource()==start_jbtn)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>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());
<span style="white-space:pre"> </span>start_jbtn.setIcon(start_imgIcon_b);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else if(e.getSource()==help_jbtn)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>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());
<span style="white-space:pre"> </span>help_jbtn.setIcon(help_imgIcon_b);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else if(e.getSource()==exit_jbtn)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>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());
<span style="white-space:pre"> </span>exit_jbtn.setIcon(exit_imgIcon_b);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override//鼠标退出组件
<span style="white-space:pre"> </span>public void mouseExited(java.awt.event.MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>//设置图片变回原来的大小
<span style="white-space:pre"> </span>//判断组件
<span style="white-space:pre"> </span>if(e.getSource()==start_jbtn)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>start_jbtn.setBounds(x, y,start_imageIcon.getIconWidth(),start_imageIcon.getIconHeight());
<span style="white-space:pre"> </span>start_jbtn.setIcon(start_imageIcon);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else if(e.getSource()==help_jbtn)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>help_jbtn.setBounds(x,y+vd+help_imageIcon.getIconHeight(),help_imageIcon.getIconWidth(),help_imageIcon.getIconHeight());
<span style="white-space:pre"> </span>help_jbtn.setIcon(help_imageIcon);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else if(e.getSource()==exit_jbtn)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>exit_jbtn.setBounds(x,y+vd+vd+2*exit_imageIcon.getIconHeight(),exit_imageIcon.getIconWidth(),exit_imageIcon.getIconHeight());
<span style="white-space:pre"> </span>exit_jbtn.setIcon(exit_imageIcon);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
}</span>
<span style="font-size:32px;">Tool.java:</span>
p<span style="font-size:18px;">ackage cn.edu.hytc;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
public class Tool {
<span style="white-space:pre"> </span>//获取图片
<span style="white-space:pre"> </span>public static ImageIcon getIcon(String filename)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//获取图片对象<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Image image=Toolkit.getDefaultToolkit().getImage(filename);
<span style="white-space:pre"> </span>ImageIcon imageIcon=new ImageIcon(image);
<span style="white-space:pre"> </span>return imageIcon;
<span style="white-space:pre"> </span>}
}</span>
<span style="font-size:24px;">//以上三类个文件可生成此图,当鼠标移动到按钮上,可看到如图二“新手指导”的效果:</span>
HelpJPanel.java
package cn.edu.hytc;
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;
private ImageIcon back_imgIcon,back_imgIcon_b;
public HelpJPanel()
{
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
}
@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);
}
}
关于信息界面:
GameJFrame2.java:
package cn.edu.hytc;
//游戏首页 selectJPanel的窗口
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameJFrame2 extends JFrame {
public selectJPanel selectJPanel;//首页面板对象
public GameJFrame2()
{
selectJPanel=new selectJPanel();
JPanel c=(JPanel)this.getContentPane();
c.add(selectJPanel);
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 GameJFrame2();
}
}
//第二个选择界面
selectJPanel.java:
package cn.edu.hytc;
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 selectJPanel()
{
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
}
@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);
}
}
}
以上两个类可实现第二个界面,方法相似,并可触发效果: