本篇博客是对飞机大战游戏首页、游戏结束代码的展示
(代码比较简单,不再做过多的解释,每行基本都有注释,可以根据注释进行理解)
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class game_plane_1 extends JFrame{
ImageIcon image=new ImageIcon("game_image/start.png");
JLabel jl=new JLabel(image);
public game_plane_1() {
jl.addMouseListener(new mouse());//添加鼠标监听器
this.add(jl);
this.setTitle("飞机大战");//窗体标题
this.setResizable(false);//窗体不可最大化
this.setSize(image.getIconWidth(),image.getIconHeight());//设置大小
this.setLocationRelativeTo(null);//窗体位置
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//窗体关闭
this.setVisible(true);//窗体可视
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new game_plane_1();
}
class mouse extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
//鼠标点击事件
if(e.getSource()==jl) {
//new game_plane_2();
//dispose();
//跳转到游戏界面,后续会有完整代码
}
}
}
}
(与首页代码类似)
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class game_plane_3 extends JFrame{
ImageIcon image=new ImageIcon("game_image/gameover.png");
JLabel jl=new JLabel(image);
public game_plane_3() {
jl.addMouseListener(new mouse());//添加鼠标监听器
this.add(jl);
this.setTitle("游戏结束");//窗体标题
this.setResizable(false);//窗体不可最大化
this.setSize(image.getIconWidth(),image.getIconHeight());//设置大小
this.setLocationRelativeTo(null);//窗体位置
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//窗体关闭
this.setVisible(true);//窗体可视
}
class mouse extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
//鼠标点击事件
if(e.getSource()==jl) {
//new game_plane_1();
//dispose();
//点击后返回首页,后续会有完整代码
}
}
}
}
下一篇:用JAVA制作小游戏——飞机大战(二)(待发布)