java GUI游戏界面问题 play,exit操作

本文代码图片路径自己要重新改,没有粘图片上来

javaGUI做游戏界面,如果用文字的话会出现界面特别卡的问题,我总结出用ps做特效字体,然后用JLable标签加进去,再对对应的标签做鼠标点击事件

1.JLable标签支持html代码,eg:JLalbe jl=new JLable("<html><body><font size='5' face='隶属'>想添加的文字</font></body></html>");

2.注意要设置组件到相应的位置必须要对窗口的整个布局设置为空,eg:this.setLocationLayout(null);否则对组件.setBounds(.....)将无效

3.有时候设背景色不管用可以用这个:

              Container  contain=getContentPane();

              contain.setBackground(Color.red);

3.添加鼠标事件有以下几种:MouseEvent表示当前活动的是哪个键

                mouseClicked(MouseEvent e);鼠标点击操作

                MouseEntered(MouseEvent e);鼠标进入操作

                MouseExited(MouseEvent d);鼠标移出操作

                MousePressed(MouseEvent e);鼠标按下操作

                MouseReleased(MouseEvent e );鼠标释放操作

  1 package demo.choose;
  2 
  3 import java.awt.Color;
  4 import java.awt.Container;
  5 import java.awt.Image;
  6 import java.awt.event.MouseAdapter;
  7 import java.awt.event.MouseEvent;
  8 
  9 import javax.swing.ImageIcon;
 10 import javax.swing.JDialog;
 11 import javax.swing.JFrame;
 12 import javax.swing.JLabel;
 13 import javax.swing.JOptionPane;
 14 
 15 import demo.start.GameStart;
 16 /**
 17  * 初始界面类
 18  * @author Administrator
 19  *
 20  */
 21 @SuppressWarnings("all")
 22 public class ChooseFunction extends JFrame{
 23     public JDialog jd=new JDialog();
 24     public void init(){
 25         this.setSize(500, 800);//设置窗口的大小
 26         //设置窗口布局为null
 27         this.setLayout(null);//把布局设置为空才能把组件位置设置为特殊位置
 28         Container contain=getContentPane();//获取整个界面的容器
 29         contain.setBackground(Color.BLACK);//设置界面的背景
 30         //加图标
 31         this.setTitle("Airplane big war!!");
 32         this.setIconImage(new ImageIcon("./src/images/icon.png").getImage());
 33         
 34         
 35         //添加图片在上面
 36         JLabel jl1=new JLabel(new ImageIcon("./src/images/play.gif"));//创建一个JLable用来放play图片
 37         JLabel jl2=new JLabel(new ImageIcon("./src/images/exit.gif"));//创建一个JLable用来放exit图片
 38         JLabel jl3=new JLabel(new ImageIcon("./src/images/war.gif"));//创建一个JLable用来放标题
 39         JLabel jl4=new JLabel(new ImageIcon("./src/images/introduction.gif"));//创建一个JLable用来放说明
 40         //设置游戏的说明
 41         jl4.setForeground(Color.RED);
 42         jl1.setBounds(160, 300, 200, 100);//开始图片的位置
 43         jl2.setBounds(160, 400, 200, 100);//退出图片的位置
 44         jl3.setBounds(0, 100,500, 120);//标题图片的位置
 45         jl4.setBounds(100, 500,319,112);//功能介绍图片的位置
 46         this.add(jl1);//把开始图片添加到框架中
 47         this.add(jl2);//把退出图片添加到框架中
 48         this.add(jl3);//把标题图盘添加到框架中
 49         this.add(jl4);////把说明图片添加到框架中
 50         //为play组件添加鼠标事件,把当前窗口传进去
 51         jl1.addMouseListener(new PlayMouseListener(this));//为play添加鼠标事件
 52         //为exit组件添加鼠标事件
 53         jl2.addMouseListener(new ExitMouseListener());//为exit添加鼠标事件
 54         //为introduction设置弹框
 55         jl4.addMouseListener(new IntroduceMouseListener(this));//为说明添加鼠标事件
 56         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭
 57         this.setLocationRelativeTo(null);//把窗口的相对屏幕的位置居中显示
 58         this.setVisible(true);//把窗口可视化
 59     }
 60     //为游戏说明设置鼠标监听事件
 61     public class IntroduceMouseListener extends MouseAdapter{
 62         
 63         //把当前窗口创建出来
 64         private ChooseFunction chooseFunction;
 65         
 66         public IntroduceMouseListener(ChooseFunction chooseFunction) {
 67             this.chooseFunction = chooseFunction;
 68         }
 69         @Override
 70         public void mouseEntered(MouseEvent e) {
 71             jd.setTitle("Airplane big war!!");//设置显示框的标题
 72             jd.setIconImage(new ImageIcon("./src/images/icon.png").getImage());//设置显示框的图标
 73              // jd.setLocationRelativeTo(getOwner());//并没什么用
 74              //jd.setLocationRelativeTo(chooseFunction);//也并没什么用
 75             jd.setBounds(415, 200, 450, 380);//设置标题框的位置和大小
 76              /* ImageIcon icon = new ImageIcon("./src/images/reduce.gif");
 77 //             icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),  
 78 //                       icon.getIconHeight(), Image.SCALE_DEFAULT));
 79              JLabel lb = new JLabel();
 80              lb.setIcon(icon);*/
 81              JLabel lb=new JLabel(new ImageIcon("./src/images/reduce.gif"));//创建一个JLable放说明的图片
 82              jd.add(lb);//把JLable添加到说明框中
 83              jd.setVisible(true);//设置说明框的可视化
 84         }
 85         @Override
 86         public void mouseExited(MouseEvent e) {
 87              jd.setVisible(false);//把说明框的可视化去掉
 88         }
 89     }
 90     //为play组件添加鼠标事件做准备
 91     public class PlayMouseListener extends MouseAdapter{
 92         //把当前窗口创建出来
 93         private ChooseFunction chooseFunction;
 94         //构造函数传值
 95         public PlayMouseListener(ChooseFunction chooseFunction) {
 96             this.chooseFunction = chooseFunction;
 97         }
 98         @Override
 99         public void mouseClicked(MouseEvent e) {
100               if(e.getButton()==1){
101                   //把当前窗口隐藏
102                   chooseFunction.setVisible(false);
103                   //调用下一个窗口的初始界面
104                   new GameStart("images/sky.gif");
105               }
106             
107         }
108     }
109     
110     //为exit组件添加鼠标事件做准备
111     public class ExitMouseListener extends MouseAdapter{
112         @Override
113         public void mouseClicked(MouseEvent e) {
114             if(e.getButton()==1){
115                int n=JOptionPane.showConfirmDialog(null, "你确定退出吗?");
116                      if(n==0){
117                      System.exit(0);
118                      }
119             }
120         }
121         
122     }
123     //主函数入口
124      public static void main(String[] args) {
125         new ChooseFunction().init();
126     }
127 }

 

posted @ 2015-07-24 12:11  wenyi1993  阅读(745)  评论(0编辑  收藏  举报