问答题

请完成下列Java程序:实现2个下拉式菜单,一个包含exit菜单项,另一个包含item1和item2共2个菜单项。要求选择exit菜单项时,退出程序;选择item1菜单项之后,item1项变为不可选而item2可选;选择item2菜单项时,item2变为不可选而item1可选。 注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 程序运行结果如下:
import java.awt.*; import java.awt.event.*; public class ex18_2 extends Frame implements ActionListener { private choiceHandler ch; private MenuItem item1; private MenuItem item2; public static void main(String[] arg) { new ex18_2 ( ); } ex18_2 ( ) { setTitle("ex18_2"); MenuItem item; ch = new choiceHandler(); MenuBar mb = new MenuBar(); Menu fm = new Menu("File"); fm.addSeparator(); fm.add(item = new MenuItem("Exit")); item.addActionListener(this); fm.add(item); mb.add(fm); Menu mm = new Menu("Choice"); mm.add(item1 = new MenuItem("item1")); item1.addActionListener(ch); mm.add(item2 = new MenuItem("item2")); item2.addActionListener(ch); mb.add(mm); setMenuBar(mb); setSize(200,200); show(); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Exit")) System.exit(0); else System.out.println(ae.getActionCommand()); } class choiceHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { String strCommand = ae.getActionCommand(); if(_________________) { item2.setEnabled(true); item1.setEnabled(false); } else if(______________________) { item2.setEnabled(false); item1.setEnabled(true); } } } }

【参考答案】

strCommand.equals(”item1”) strCommand.equals(”item2”)[解析] 本题......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
下面是一个Applet程序,其功能是根据公式:y=a*sin(x)绘制正弦曲线。要求窗口中有一个文本区作为曲线峰值a的输入,可以判断输入的数字是否符合要求,一个ok按钮,点击则在画布中绘制曲线,以画布中的横坐标值作为sin()的参数x。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:import java.awt.*;import java.awt.event.*;import java.applet.Applet;import java.lang.Math.*; *<applet code= ex18_3.class width=800 height=400 >< applet>* public class ex18_3 extends Applet implements ActionListener {Panel pane=new Panel();drawWnd dw;Label 11 = new Label( 峰值 );TextField tf1 = new TextField(15);Button btn = new Button( OK );private float a=0;public void init(){pane.setLayout(new FlowLayout(FlowLayout.CENTER,25,5));pane.add(11);pane.add(tf1);pane.add(btn);btn.addActionListener(this);add( North ,pane);dw=new drawWnd();add( South ,dw);}class drawWnd extends Canvas {drawWnd() {setSize(300,100);setBackground(Color. GRAY);}public void paint(Graphics g) {g.setColor(Color.GREEN);for(int i=0;i<getSize().width-1;i++){int y=0;int y1=y+(int) (Math.sin(i)*a);int y2=y1+(int) (a*Math.sin(i+1));g.drawLine(i,y1,i+1,y2);}}}public void actionPerformed(ActionEvent ae) {try{a=Integer.parseInt(tf1.getText());dw.repaint();}catch(NumberFormatException nfe){tf1.setText( error! );}}}ex18_3.html<HTML><HEAD><TITLE>ex18_3< TITLE>< HEAD><BODY><applet code= ex18_3.class width=800 height=400 >< applet>< BODY>< HTML>