?? gameframe2.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 + -