问题 问答题

本题中,通过菜单“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");
connectItem.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 connectItem;
private JMenuItem exitItem;

class ConnectInfo
public String username;
public String password;
public Connectfnfo(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(false);

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 ConneetDialog(JFrame parent)
第3处:public Boolean showDialog(ConnectInfotransfer)

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

问答题

Ⅰ、CO和H2的混合气体俗称合成气,是一种重要的工业原料气,焦炭、天然气(主要成分为CH4)、重油、煤在高温下均可与水蒸气反应制得合成气.

(1)已知某反应的平衡常数表达式为:K=

c(H2)•c(CO)
c(H2O)
,它所对应的化学方程式为:______.

(2)向体积为2L的密闭容器中充入CH4和H2O(g)组成的混合气(共1mol),在一定条件下发生反应,并达到平衡状态,测得温度、压强、投料比X〔

n(CH4)
n(H2O)
)对该反应的影响如图所示.

①图1中的两条曲线所示投料比的关系X2______X1(填“=”“>”或“<”下同)

②图2中两条曲线所示的压强比的关系:P2______P1

(3)以CH4、O2为原料可设计成燃料电池:

①设装置中盛有150.0mL1.0mol/LKOH溶液,放电时参与反应的氧气在标准状况下的体积为4.48L,放电过程中转移电子的物质的量为______.

②以H2SO4溶液代替KOH溶液为电解质溶液,则燃料电池的负极反应式为:______.

Ⅱ、(1)有一种用CO2生产甲醇燃料的方法:

已知:CO2(g)+3H2(g)⇌CH3OH(g)+H2O(g)△H=-akJ•mol-1

CH3OH(g)═CH3OH(l)△H=-bkJ•mol-1

2H2(g)+O2(g)═2H2O(g)△H=-ckJ•mol-1

H2O(g)═H2O(l)△H=-dkJ•mol-1

则表示CH3OH(l)燃烧热的热化学方程式为:______.

(2)已知某温度下Mg(OH)2的Ksp=6.4xl0-12,当溶液中c(Mg2+)≤1.0×10-5mol•L-1可视为沉淀完全,则应保持溶液中c(OH-)≥______mol•L-1

选择题