3、[ 主观题 ]【程序填空题】设计一个JFrame窗口,窗口中心有一个文本区。另有4个菜单,每个菜单都有菜单项,每个菜单项都对应有快捷键,选择某个菜单项时,窗口中心的文本区显示相应信息。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class C2902 implements ActionListener
{
___
JTextField text=new JTextField(20);
String t[]={"文件","编辑","格式","帮助"};
Menu menu[]=new Menu[4];
String s[]={"打开","保存","复制","粘贴","字体","段落","小助手","关于"};
MenuItem jmi[]=new MenuItem[8];
C2902()
{
JFrame myWin=new JFrame("Ex0606");
myWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=myWin.getContentPane();
con.add(text,"Center");
text.setEditable(false);
myWin.setMenuBar(jmb);
int i;
for(i=0;i<4;i++)//生成四个菜单
{
menu[i]=new Menu(t[i]);
jmb.add(menu[i]);
}
for(i=0;i<8;i++)//每个菜单中放两个菜单项
{
jmi[i]=new MenuItem(s[i]);
menu[i/2].add(jmi[i]);
jmi[i].addActionListener(this);
jmi[i].___(new MenuShortcut(65+i));//设快捷键
}
myWin.setBounds(300,200,400,200);
myWin.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
text.setText("你选择了:"+e.getActionCommand()+"菜单项.");
}
public static void main(String[] args)
{
new C2902();
}
}