




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
当选择使用JPanel和顶层容器的content pane时,需要考虑布局管理。JPanel缺省是初始化一个FlowLayout,而content pane缺省是初始化一个BorderLayout。下面将分别介绍几种最常用的布局管理器:FlowLayout、BorderLayout、BoxLayout、CardLayout、GridLayout和GridBagLayout。代码演示每一个布局管理器都会有一个代码演示,xxxLayoutDemo.java(见附件)。这些文件主要有三个方法组成:addComponentsToPane()提供布局逻辑(依据不同的布局管理器和UI内容)。publicstaticvoidaddComponentsToPane(Container pane) 。createAndShowGUI()实例化一个JFrame,通过它的ContentPane加载布局逻辑内容。privatestaticvoidcreateAndShowGUI() / Create and set up the window.JFrame frame =newJFrame(FlowLayoutDemo);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/ Set up the content pane.addComponentsToPane(frame.getContentPane();/ Display the window.frame.pack();frame.setVisible(true);main()程序入口,单独起一个线程,实例化UI。publicstaticvoidmain(String args) javax.swing.SwingUtilities.invokeLater(newRunnable() publicvoidrun() createAndShowGUI(););FlowLayoutFlowLayout类是最简单的布局管理器。它按照和页面上排列单词的类似方式来安排组件-从左到右,直至没有多余的空间,然后转到下一行。效果:内容面板代码:publicstaticvoidaddComponentsToPane(Container pane) pane.setLayout(newFlowLayout();pane.add(newJButton(Button 1);pane.add(newJButton(Button 2);pane.add(newJButton(Button 3);pane.add(newJButton(Long-Named Button 4);pane.add(newJButton(5);BorderLayout一个BorderLayout对象将界面分成五大区域,分别用BorderLayout类的静态常量指定:-PAGE_START-PAGE_END-LINE_START-LINE_END-CENTER效果:内容面板代码:publicstaticvoidaddComponentsToPane(Container pane) JButton button =newJButton(Button 1 (PAGE_START);pane.add(button, BorderLayout.PAGE_START);button =newJButton(Button 2 (CENTER);button.setPreferredSize(newDimension(200, 100);pane.add(button, BorderLayout.CENTER);button =newJButton(Button 3 (LINE_START);pane.add(button, BorderLayout.LINE_START);button =newJButton(Long-Named Button 4 (PAGE_END);pane.add(button, BorderLayout.PAGE_END);button =newJButton(5 (LINE_END);pane.add(button, BorderLayout.LINE_END);BoxLayoutBoxLayout可以将组件由上至下或由左至右依次加入当前面板。效果:内容面板代码:publicstaticvoidaddComponentsToPane(Container pane) JPanel xPanel =newJPanel();xPanel.setLayout(newBoxLayout(xPanel, BoxLayout.X_AXIS);addButtons(xPanel);JPanel yPanel =newJPanel();yPanel.setLayout(newBoxLayout(yPanel, BoxLayout.Y_AXIS);addButtons(yPanel);pane.add(yPanel, BorderLayout.PAGE_START);pane.add(xPanel, BorderLayout.PAGE_END);privatestaticvoidaddAButton(String text, Container container) JButton button =newJButton(text);button.setAlignmentX(Component.CENTER_ALIGNMENT);container.add(button);privatestaticvoidaddButtons(Container container) addAButton(Button 1, container);addAButton(Button 2, container);addAButton(Button 3, container);addAButton(Long-Named Button 4, container);addAButton(5, container);CardLayout卡片布局和其他布局不同,因为它隐藏了一些组件。卡片布局就是一组容器或者组件,它们一次仅仅显是一个,组中的每个容器称为卡片。效果:内容面板代码:publicvoidaddComponentToPane(Container pane) finalJPanel contentPanel =newJPanel();JPanel controlPanel =newJPanel();finalCardLayout cardLayout=newCardLayout();pane.setLayout(newBorderLayout();pane.add(contentPanel, BorderLayout.CENTER);pane.add(controlPanel, BorderLayout.PAGE_END);controlPanel.setLayout(newFlowLayout();JButton b =newJButton10;for(inti = 0; i 10; i+) bi =newJButton(No.+ i);contentPanel.add(bi);contentPanel.setLayout(cardLayout);JButton nextButton =newJButton(next);nextButton.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvent e) cardLayout.next(contentPanel););controlPanel.add(nextButton);GridLayoutGridLayout让你建立一个组件表格,并且当组件加入时,会依序又左至右,由上至下填充到每个格子,它不能由你指定想放那个格子就放那个格子效果:内容面板代码:publicstaticvoidaddComponentsToPane(Container pane) JButton buttons =newJButton9;pane.setLayout(newGridLayout(3, 3);for(inti = 0; i buttons.length; i+) buttonsi =newJButton(i +);pane.add(buttonsi);GridBagLayoutGridBagLayout是所有AWT布局管理器当中最复杂的,同时他的功能也是最强大的。GridBagLayout同GridLayout一样,在容器中以网格形式来管理组件。但GridBagLayout功能要来得强大得多。1、GridBagLayout管理的所有行和列都可以是大小不同的;2、GridLayout把每个组件限制到一个单元格,而GridBagLayout并不这样:组件在容器中可以占据任意大小的矩形区域。GridBagLayout通常由一个专用类来对他布局行为进行约束,该类叫GridBagConstraints。其中有11个公有成员变量,GridBagConstraints可以从这11个方面来进行控制和操纵。这些内容是:1、gridx组件的横向坐标;2、girdy组件的纵向坐标;3、gridwidth组件的横向宽度,也就是指组件占用的列数;4、gridheight组件的纵向长度,也就是指组件占用的行数;5、weightx指行的权重,告诉布局管理器如何分配额外的水平空间;6、weighty指列的权重,告诉布局管理器如何分配额外的垂直空间;7、anchor当组件小于其显示区域时使用此字段;8、fill如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充;9、insets指组件与表格空间四周边缘的空白区域的大小;10、ipadx组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值;11、ipady组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值。说明:1、gridx,gridy:其实就是组件行列的设置,注意都是从0开始的,比如gridx=0,gridy=1时放在0行1列;2、gridwidth,gridheight:默认值为1;GridBagConstraints.REMAINDER常量,代表此组件为此行或此列的最后一个组件,会占据所有剩余的空间;3、weightx,weighty:当窗口变大时,设置各组件跟着变大的比例。比如组件A的weightx=0.5,组件B的weightx=1,那么窗口X轴变大时剩余的空间就会以1:2的比例分配给组件A和B;4、anchor:当组件空间大于组件本身时,要将组件置于何处。有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。5、insets:设置组件之间彼此的间距。它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。效果:内容面板代码:publicstaticvoidaddComponentsToPane(Container pane) JButton button;pane.setLayout(newGridBagLayout();GridBagConstraints c =newGridBagConstraints();button =newJButton(Button 1);c.fill= GridBagConstraints.HORIZONTAL;c.gridx= 0;c.gridy= 0;pane.add(button, c);button =newJButton(Button 2);c.fill= GridBagConstraints.HORIZONTAL;c.weightx= 0.5;c.gridx= 1;c.gridy= 0;pane.add(button, c);button =newJButton(Button 3);c.fill= GridBagConstraints.HORIZONTAL;c.weightx= 0.5;c.gridx= 2;c.gridy= 0;pane.add(button, c);button =newJButton(Long-Named Button 4);c.fill= GridBagConstraints.HORIZONTAL;c.ipady= 40;/ make this component tallc.weightx= 0.0;c.gridwidth= 3;c.gridx= 0;c.gridy= 1;pane.add(button, c);button =newJButton(5);c.fill= GridBagConstraints.HORIZONTAL;c.ipady= 0;/ reset to defaultc.weighty= 1.0;/ request any extra vertical spacec.anchor= GridBagConstraints.PAGE_END;/ bottom of spacec.insets=newInsets(10, 0, 0, 0);/ top paddingc.gridx= 1;/ aligned with button 2c.gridwidth= 2;/ 2 columns widec.gridy= 2;/ third rowpane.add(button, c);一个GardBagLayout布局的左右选择框,代码GridBagLayoutFrame.java见附件,效果:GridBagLayoutFrame.java附件源码import java.awt.Dimension;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.BorderFactory;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JTextField;public class GridBagLayoutFrame extends JFrame private static final long serialVersionUID = 6819222900970457455L;private JPanel mainPanel = new JPanel();private JButton addButton = new JButton();private JButton leftButton = new JButton();private JButton rightButton = new JButton();private JLabel label = new JLabel();private JTextField field = new JTextField();private DefaultListModel leftModel = new DefaultListModel();private DefaultListModel rightMOdel = new DefaultListModel();private JList leftList = new JList(leftModel);private JList rightList = new JList(rightMOdel);public GridBagLayoutFrame(String title) setTitle(GridBagLayoutFrameDemo);setPreferredSize(new Dimension(600, 400);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);initComponent();addData();pack();setVisible(true);private static void createAndShowGUI() new GridBagLayoutFrame(GridBagLayoutFrameDemo);private void initComponent() label.setText(添加选项:);addButton.setText(添加);leftList.setPreferredSize(new Dimension(150, 150);rightList.setPreferredSize(leftList.getPreferredSize();leftButton.setText(左);rightButton.setText(右);mainPanel.setBorder(BorderFactory.createTitledBorder(左右选择框);mainPanel.setLayout(new GridBagLayout();GridBagConstraints c = new GridBagConstraints();c.gridx = 0; / 0行0列c.gridy = 0;c.gridwidth = 1;c.gridheight = 1;c.fill = GridBagConstraints.HORIZONTAL;c.weightx = 0;c.weighty = 0;mainPanel.add(label, c);c.gridx+;c.weightx = 1;mainPanel.add(field, c);c.gridx+;c.weightx = 0;c.gridwidth = 1;c.gridheight = 1;/ c.fill = GridBagConstraints.HORIZONTAL;mainPanel.add(addButton, c);c.gridx = 0;c.gridy = 1;c.weightx = 1;c.weighty = 1;c.gridwidth = 2;c.gridheight = 2;c.fill = GridBagConstraints.BOTH;mainPanel.add(leftList, c);c.gridx = 2;c.gridy = 1;c.gridwidth = 1;c.gridheight = 1;c.weightx = 0;c.weighty = 0.5;c.anchor = GridBagConstraints.SOUTH;c.fill = GridBagConstraints.HORIZONTAL;mainPanel.add(leftButton, c);c.gridx = 2;c.gridy = 2;c.anchor = GridBagConstraints.NORTH;c.fill = GridBagConstraints.HORIZONTAL;mainPanel.add(rightButton, c);c.gridx = 3;c.gridy = 1;c.gridwidth = 1;c.gridheight = 2;c.weightx = 1;c.weighty = 1;c.fill = GridBagConstraints.BOTH;mainPanel.add(rightList, c);this.getContentPane().add(mainPanel);private void addData() addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) addItem(););leftButton.addActionListener(new ActionListener() public void actionPerfo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 民法学习课件
- 北京舞蹈考试题库及答案
- 结核病治疗方案
- 人工智能:新质生产力的技术引擎
- 打造新质生产力的人才磁场
- 乡镇过去五年进展工作总结及将来五年工作方案
- 新质生产力本质属性
- 幼儿园班级疫情防控活动方案
- 干部如何服务新质生产力发展
- 新质生产力看莞深智造
- 2024-2030年中国电解二氧化锰(EMD)行业市场发展趋势与前景展望战略分析报告
- 初中+物理运动的描述++人教版八年级物理上册+
- 计算机科学实习合同模板
- 人身损害三期评定规范
- 《我与地坛》教学设计 统编版高中语文必修上册
- 工业固废运输处置投标方案(技术标)
- 上海市语文新初一均衡分班试卷
- 中医培训课件:《放血疗法》
- KA-T 20.1-2024 非煤矿山建设项目安全设施设计编写提纲 第1部分:金属非金属地下矿山建设项目安全设施设计编写提纲
- 医务人员职业暴露的预防与处理应急预案
- 《古建筑构件制作(榫卯、斗拱)》课程标准
评论
0/150
提交评论