Java2中的框架种和容器类、容器类与布局类的聚合关系

Java2中的框架类和容器类、容器类与布局类的聚合关系

一: 布局中的JFrame与JPanel、Container与各Layout之间的聚合关系
/*<pre>伪源代码*/
//DemoFlowLayout类先用主main方法调用了类的构造函数,启动进程。
public static void main(String args[]){
 JFrame theFrame=new DemoFlowLayout();
theFrame.setSize(200,125); 
 //也可以用在DemoFlowLayout()构造函数中this.setSize(..,..);this.setVisible(true);
//如果是JPanel还要,this.add(jPanel); 这也是聚合关系。

theFrame.setVisible(true);
}
//声明了控件变量。
public DemoFlowLayout(){
//set title
setTitle("FlowLayout Demo");
//Create container and layout
Container contentPane=getContentPane();
FlowLayout layout=new FlowLayout();
contentPane.setLayout(layout);
/*类似I/O中聚合关系 用FileInputStream做参数传递给ObjectInputStream后,objectInputStream.readObject();
此处也为contentPane嵌套layout后,contentPane聚合了layout对象,作为一个整体实现
.add controls
*/

//add controls to container
contentPane.add(new JLable("Frahrenheit");
contentPane.add(new JTextField("212",6);
contentPane.add(new JLable("Celsius");
contentPane.add(new JTextField("100",6);
JButton btFtoC=new JButton("F to C");
JButton btCtoF=new JButotn("C to F");
contentPane.add(btFtoC);
contentPane.add(btCtoF);
btFtoC.addActionListener(new FtoCListener());   //解析的地方
btCtoF.addActionListener(new CtoFListener());
addWindowListener(new MyWindowAdapter());
}
private class FtoCListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String inStr=tfFahrenheit.getText().trim();
double f=Double.parseDouble(inStr);
thermo.setFahrenheit(f);
String outStr=Format.justify('l',thermo.getCellsius(),0,2);
tfCelsius.setText(outStr);
}
}
private class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}