问答题

本题程序的功能是利用单选按钮选择性别,并在下面显示用户的选择结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
______;
public class simple

public static void main (String args[])

Frame f = new Frame ("simple");
f.setLayout (new GridLayout(3,1));
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)

System.exit (0);

);
CheckboxGroup cg = new CheckboxGroup();
Checkbox male = new Checkbox("男",cg,false);
Checkbox female = new Checkbox("女",cg,false);
final Label la = new Label("请选择你的性别");
male.addItemListener(new ItemListener()
public void itemStateChanged(ItemEvent e)

la.setText ("你是个男生");

);
female.addItemListener(new ItemListener()
public void itemStateChanged(ItemEvent e)

la.setText ("你是个女生");

);
f.add(male);
f.add(female);
f.add(la);
f.setSize(200,200);
______;

【参考答案】

importjava.awt.event.*。
f.setVisible(true)。
热门 试题

问答题
本题程序的功能是监听对于列表项的操作。窗口中有一个列表和“添加”、“删除”和“关闭”三个按钮。单击“添加”按钮,会在当前所选列表项后添加一个名为“新增表项”的列表项,同时后台输入列表中的表项数量。单击“删除”按钮,如果未选中表项,则弹出提示消息框“请选择表项”,否则将选中的表项删除,同时后台输出删除表项的内容和列表中的表项数量。单击“关闭”按钮退出程序。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; public class advance public static void main(String args[]) final Frame frmFrame = new Frame(); Panel pnlPanel1 = new Panel(); Panel pnlPanel2 = new Panel(); final List istList = new List(8); for(int i = 0; i <10; i++) String strName = 表项 + (new Integer(i+1)).toString(); istList.add( strName ); Button btnButton1 = new Button( 添加 ); Button btnButton2 = new Button( 删除 ); Button btnButton3 = new Button( 关闭 ); btnButton1.addActionListener( new ActionListener() publiC void actionPerformed(ActionEvent e) lstList.add( 新增表项 ,istList.getSelected()+1); System.out.println( 列表中的表项数量 + istList,getItemCount() ); ); btnButton2.addActionListener (new ActionListener () public void actionPerformed(ActionEvent e) if(istList.getSelected() == null) JOptionPane.showMessageDialog(frmFrame, 请选择表项 ); return; System.out.println( 删除表项的内容: + istList,getSelectedItem()); istList.delete(istList.getSelectedIndex()); System.out.println( 列表中的表项数量: + istList,getItemCount ()); ); btnButton3.addActionListener(new ActionListener () public void actionPerformed(ActionEvent e) System.exit (0); ); pnlPanel1 .add (istList); pnlPanel2 .add (btnButton1); pnlPanel2 .add (btnButton2); pnlPanel2,add (btnButton3); frmFrame.add( North ,pnlPanel1); frmFrame.add( South ,pnlPanel2); frmFrame,setTitle ( advance ); frmFrame.pack (); frmFrame,show ();