📄 eventhandleframe(1).java
字号:
package eventhandle;
/**
* Java语言实验参考程序
* Company 北京师范大学计算机系
* @author 孙一林
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
public class EventHandleFrame extends Frame {
MenuBar menuBar = new MenuBar();
Menu menu1 = new Menu();
MenuItem menuItem1 = new MenuItem();
Menu menu2 = new Menu();
MenuItem menuItem2 = new MenuItem();
public EventHandleFrame() {
try {
Init();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
EventHandleFrame eventHandleFrame = new EventHandleFrame();
eventHandleFrame.setSize(400,300);
eventHandleFrame.show();
}
private void Init() throws Exception {
this.setTitle("菜单事件响应");
this.setMenuBar(menuBar);
menu1.setLabel("File");
menuItem1.setLabel("Exit");
menu2.setLabel("Help");
menuItem2.setLabel("About");
menuBar.add(menu1);
menuBar.add(menu2);
menu1.add(menuItem1);
menu2.add(menuItem2);
/* ============== 开始添加菜单事件监听器和适配器类代码 ============== */
menuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItem1_actionPerformed(e);
}
});
menuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItem2_actionPerformed(e);
}
});
/* ============== 添加菜单事件监听器和适配器类代码结束 ============== */
}
/* ================= 开始添加菜单事件处理方法代码 =================== */
void menuItem1_actionPerformed(ActionEvent e) {
dispose();
System.exit(1);
}
void menuItem2_actionPerformed(ActionEvent e) {
AboutDialog aboutDialog = new AboutDialog(this, "About", true);
aboutDialog.setSize(400,300);
aboutDialog.show();
}
/* ================= 添加加菜单事件处理方法代码结束 ================== */
}
/* ======================= About对话框类 ========================= */
class AboutDialog extends Dialog {
Panel panel1 = new Panel();
Label label = new Label();
Button button = new Button();
public AboutDialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
Init();
add(panel1);
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public AboutDialog(Frame frame) {
this(frame, "", false);
}
public AboutDialog(Frame frame, boolean modal) {
this(frame, "", modal);
}
public AboutDialog(Frame frame, String title) {
this(frame, title, false);
}
void Init() throws Exception {
label.setText("Copyright(c) 2003");
label.setBounds(new Rectangle(108, 92, 164, 46));
panel1.setLayout(null);
button.setLabel("OK");
button.setBounds(new Rectangle(137, 198, 84, 31));
panel1.add(button, null);
panel1.add(label, null);
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
cancel();
}
super.processWindowEvent(e);
}
void cancel() {
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -