⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameframe2.java

📁 java小游戏代码
💻 JAVA
字号:
// 程序:AWT组件事件处理
// 范例文件:GameFrame2.java

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

class GameFrame2 extends Frame 
      implements WindowListener,ActionListener
{
   Panel    main,top,center;                //main将包含top与center
   Menu     MU;                             //主菜单项
   MenuBar  MB;                             //菜单栏
   MenuItem MI1,MI2;                        //菜单项

   InfoDialog   DW;                         //对话窗口
   
   public GameFrame2(String Title,int AppletWidth,int AppletHeight,
                    Applet Game)
   {
      super(Title);                         //建立窗口
      main      = new Panel();              //建立Panel
      top       = new Panel();
      center    = new Panel();

      //建立对话窗口
      DW        = new InfoDialog(this,"基本信息",true);

      addWindowListener(this);

      main.setLayout(new BorderLayout());   //main使用BorderLayout
      center.setLayout(new CardLayout());   //center使用CardLayout
                                            //top使用预设的FlowLayout

      main.add(top,BorderLayout.NORTH);     //将top置入main上方
      main.add(center,BorderLayout.CENTER); //将center置入main中央

      top.add(new Button("按钮1"));         //在top中置入两个按钮
      top.add(new Button("按钮2"));

      center.add(Game,"main");              //在center中置入Applet
      add(main);                            //在窗口中置入main

      //建立菜单
      MB = new MenuBar();
      MU = new Menu("选项");
      MU.add(MI1 = new MenuItem("基本信息"));
      MU.add(MI2 = new MenuItem("离开游戏"));
      MB.add(MU);
      setMenuBar(MB);                       //将菜单加入窗口中

      MI1.addActionListener(this);          //替选项注册事件处理方法
      MI2.addActionListener(this);
      
      setResizable(false);                     //不可改变窗口大小
      setSize(AppletWidth,AppletHeight + 100); //为何高度要加100
      show();                                  //显示窗口             
   }

   //=====实现WindowListener界面============================
   public void windowClosing(WindowEvent e)
   { 
      dispose();                               //关闭窗口
   }

   public void windowOpened(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}

   //=====实现ActionListener界面=======================================
   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource() == MI1)          //当选项1被按下时
      {
         DW.show();                     //显示对话窗口
      }   
      else if(e.getSource() == MI2)     //当选项2被按下时
         dispose();                     //关闭窗口
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -