问题 填空题

本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出来。
import java. awt. * ;
import java. awt. event. * ;
import javax. swing. * ;
public class java3 extends JFrame implements ActionListener
public java3()
setTitle("java3");
setSize(300,300);
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System. exit(0);

);
JMenuBar mbar=new JMenuBar();
setJMenuBar(bar);
JMenu fileMenu=new JMenu("File");
mbar. add(fileMenu);
connectItem=new JMenuItem("Connect");
connectltem. addActionListener(this);
fileMenu. add(connectItem);
exitItem=new JMenuItem("Exit");
exitItem. addActionListener(this);
fileMenu. add(exitItem);

public void actionPerformed(ActionEvent evt)
Object source=evt. getSource();
if (source==connectItem)
ConnectInfo transfer=new ConnectInfo("yourname", "pw");
if(dialog==null)
dialog=new ConnectDialog(this);
if(dialog. showDialog(transfer))
String uname=transfer. username;
String pwd=transfer. password;
Container contentPane=getContentPane();
contentPane. add(new JLabel("username="+uname+", password="+pwd), "South");
validate();


else if(source==exitItem)
System. exit(0);

public static void main(String[] args)
JFrame f=new java3();
f. show();

private ConnectDialog dialog=null;
private JMenuItem connectltem;
private JMenuItem exitItem;

class Connectlnfo
public String username;
public String password;
public ConneetInfo(String u, String p)
username=u; password=p;


class ConnectDialog extends JDialog implements ActionListener
public ConnectDialog()
super(parent, "Connect", true);
Container contentPane=getContentPane();
JPanel p1=new JPanel();
p1. setLayout(new GridLayout(2,2));
p1. add(new JLabel("User name:"));
p1. add(username=new JTextField(""));
p1. add(new JLabel("Password:"));
p1. add(password=new JPasswordField(""))
contentPane. add("Center", p1);
Panel p2=new Panel();
okButton=addButton(p2,"Ok");
cancelButton=addButton(p2, "Cancel");
contentPane. add( "South", p2);
setSize(240,120);

JButton addButton(Container c, String name)
JButton button=new JButton(name);
button. addActionListener(this);
c. add(button);
return button;

public void actionPerformed(ActionEvent evt)
Object source=evt. getSource();
if(source==okButton)
ok=true;
setVisible(false);

else if (source==cancelButton)
setVisible(faIse);

public void showDialog(ConnectInfo transfer)
username. setText(transfer. username);
password. setText(transfer. password);
ok=false;
show();
if(ok)
transfer. username=username, getText();
transfer. password=new String(password. getPassword());

return ok;

private JTextField username;
private JPasswordField password;
private boolean ok;
private JButton okButton;
private JButton cancelButton;

答案

参考答案:第1处:setJMenuBar(mbar)
第2处:public ConnectDialog(JFrame parent)
第3处:public Boolean showDialog(C0nnectlnfotransfer)

解析: 第1处参数错误,bar未定义;第2处从下一行的super(parent, "Connect", true);可以看出,这里需要的参数为父窗体;第3处从下面的return ok;等可以看出,这是一个有Boolean型返回值的函数,故类型应为Boolean。

阅读理解

Li Shizhen was born in Hubei Province. His father was a doctor. He learned a lot about medical herbs(草药)from his father and read many medical books. At the age of 23 he became a doctor. He often treated(治疗)poor people’s illness, so many peasants and fishermen made friends with him.

The year he was 35 Li Shizhen began to write the COMPENDIUM OF MATERIA MEDICA. He walked all over famous mountains which grew medical plants to learn their shapes(形状), the conditions in which they grew and collected all sorts of specimens(标本). He spent 27 years in writing the book.

THE COMPENDIUM OF MATERIA MEDICA with over 1,000,000 words, describes(描述)1,800 kinds of medical plants. When the work had just been ready for printing and publishing(出版), Li Shizhen passed away.

The COMPENDIUM OF MATERIA MEDICA introduces not only medical herbs growing China but also many foreign medicines. After its publication the books reached foreign countries. It was translated into English, French, and Russian and became an important piece of literature in international medical research.

61. Why did many peasants and fishermen make friends with Li Shizhen? Because

he __________.

A. was much concerned(关心)for the poor      B. had a sweet manner

C. was a famous doctor                             D. read many medical books

62. His Compendium of Materia Medica describes __________.

A. over 1,000,000 words                                               B. 1,800 kinds of medical stories

C. a lot of medical work                                               D. many foreign medical stories

63. Before his book could be____________he had died.

A. printed and published                                           B. introduced abroad

C. translated into foreign languages                                 D. completed finally

64. The word “literature” in the article means __________.

A. work of different science                                        B. letters from foreign countries

C. hope for the better future                                        D. books on a special subject

65. What’s the Chinese name of the COMPENDIUM OF MATERIA MEDICA?

A. 本草钢目            B. 四库全书            C. 天工开物            D. 齐民要术

单项选择题