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

本文介绍了一个使用FlowLayout布局管理器的Java Swing示例程序。该程序通过JFrame和JPanel组件展示了如何设置窗口布局,并添加了文本框、标签及按钮等控件。通过实例演示了如何将布局管理器与GUI组件相结合。

一: 布局中的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);
}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值