JAVA布局管理器

来源:互联网 发布:c语言 new 编辑:程序博客网 时间:2024/06/06 01:48

JAVA布局管理器分为FlowLayout, BorderLayout, CardLayout, GridLayout等,每个Frame对象可以分配一种布局管理模式。

使用BorderLayout的时候,界面被划分成上下左右中心五个部分,对应英文里的(North, South, West, East, Center),每一个部分还可以嵌套一个Panel控件,

Panel控件也可以应用布局管理器,所以在JAVA设计中,复杂的界面往往是有多种布局管理模式嵌套而成。

下面贴一段代码,综合演示了JAVA布局管理器的使用。

package layout;import java.awt.*;import java.awt.event.*;/** * 聊天室界面模拟,演示布局管理器的综合使用 */public class GridLayoutUse extends  Frame implements WindowListener{public GridLayoutUse(){super("在线聊天室");this.addWindowListener(this);initUI();}/** * 初始化界面 */private void initUI(){Panel pNorth = initNorth();this.add(pNorth, BorderLayout.NORTH);Panel pEast  = initEast();this.add(pEast, BorderLayout.EAST);Panel pCenter = initCenter();this.add(pCenter, BorderLayout.CENTER);this.setSize(450,500);this.setVisible(true);}/** * 初始化界面上部 */private Panel initNorth(){Panel p = new Panel();Label lbTitle = new Label("聊天室");p.add(lbTitle);p.setBackground(Color.PINK);return p;}/** * 初始化界面右侧 */private Panel initEast(){Panel p = new Panel();BorderLayout bl = new BorderLayout();p.setLayout(bl);Label lbTitle = new Label("当前在线");List listUser = new List();listUser.add("所有人");listUser.add("Miao");listUser.add("Wang");p.add(lbTitle, BorderLayout.NORTH);p.add(listUser,BorderLayout.CENTER);p.setBackground(Color.CYAN);return p;}/** * 初始化主界面中心 * @return 主界面中心面板 */private Panel initCenter(){Panel p = new Panel();BorderLayout bl = new BorderLayout();p.setLayout(bl);TextArea ta = new TextArea();ta.append("You enter the chatroom.\n");ta.append("You say \"Hi\" to everyone.");ta.setEditable(false);ta.setBackground(Color.ORANGE);p.add(ta,BorderLayout.CENTER);//initialize southPanel pSouth = new Panel();BorderLayout bl2 = new BorderLayout();pSouth.setLayout(bl2);Choice ch = new Choice();ch.add("All");ch.add("Wang");pSouth.add(ch, BorderLayout.WEST);TextField tf = new TextField(20);pSouth.add(tf, BorderLayout.CENTER);Button btn = new Button("Send");pSouth.add(btn, BorderLayout.EAST);p.add(pSouth, BorderLayout.SOUTH);return p;}public static void main(String[] agrs){GridLayoutUse crf = new GridLayoutUse();}@Overridepublic void windowActivated(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("activated");}@Overridepublic void windowClosed(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("Closed");}@Overridepublic void windowClosing(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("Closing");System.exit(0);}@Overridepublic void windowDeactivated(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("deactivated");}@Overridepublic void windowDeiconified(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("deiconified");}@Overridepublic void windowIconified(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("iconified");}@Overridepublic void windowOpened(WindowEvent arg0) {// TODO Auto-generated method stubSystem.out.println("open");}}

运行后代码界面效果如下:

 


 

 

此处只写了界面,没有完善功能。