Java布局管理器使用方法探讨

来源:互联网 发布:什么是数据铁笼? 编辑:程序博客网 时间:2024/05/16 19:16
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>  很多初学者在用Java布局器自动布局画界面时,经常遇见不知道如何定义区域大小或按钮之间的距离等问题。我写过一篇《实现JAVA手动布局中各个组件能随窗口变化的方法》的文章,有读者反映算坐标不好算,问能不能用布局器实现文章中的界面。其实自动布局也可以解决定义区域大小或按钮之间的距离等问题,只是没有手动布局那么灵活。下面我就举一个例子。  首先,建一个frame文件(Application应用程序),在Design中将this中的layout设置为BorderLayout。  第二,在组件盘内点选Swing Container页签,选取Jpanel图标,在this中上方拖拽一块区域,布局器会自动调整位置与大小;同样的方法在中下方也拖拽一块区域;在Swing Container页签,选取jScrollPane图标,将jScrollPane在中间拖拽一块区域。拖拽的顺序一定要先上后下再中间。为了方便区分,在Properties的background中,将上方的Jpanel1区域设置为红色,下方的Jpanel2区域设置为橙色,中间的jScrollPane1为粉红色。将Jpanel1和Jpanel2的layout设置为flowLayout(必须要手动设置,不要采用默认值)。  第三,在Jpanel中放入一个Jlable标题栏,JTextField1文本框和Jbutton按钮,在组件盘内点选Swing 页签,选取JLable图标在Jpanel1的中画一个标题栏,将text改为“请输入查询条件”,再选取JtextField在Jpanel1中画一个文本框,将text改为空,最后选取Jbutton在Jpanel1中再画一个按钮将text改为“查询”。画完后他们都是在中间,而且大小固定,这时点选Jpanel的flowLayout1将右边Properties中的alignment设置为LEFT,这时Jpanel1中的组键就会向左排列。选中其中一个组键,在Properties中的preferredSize可以设置组键的宽和高。同样的方法在Jpanel2中画三个Jbutton按钮,将text分别设为“增加”、“删除”、“修改”。点选Jpane2的flowLayout2将右边Properties中的hgap设置为30(按钮的间距,可根据自己的需要调整数值大小), 这样就调整了三个按钮之间的距离,设置vgap还可以改变Jpane2区域的高度。  第四,在jScrollPane1中建一个表格用来显示数据库数据的内容,在组件盘内点选Swing 页签,选取JTable图标,将Jtable加入到jScrollPane1中。  最后,将this中的defaultCloseOperation改为EXIT_ON_CLOSE,这样在关闭窗口时程序会自动退出。  程序源代码如下(除中文注释部分的两句是自己加上去,其余是自动生成):import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.Vector;import javax.swing.table.DefaultTableModel; public class Frame1extends JFrame {BorderLayout borderLayout1 = new BorderLayout();JPanel jPanel1 = new JPanel();JPanel jPanel2 = new JPanel();JPanel jPanel3 = new JPanel();JLabel jLabel1 = new JLabel();JTextField jTextField1 = new JTextField();JButton jButton1 = new JButton();FlowLayout flowLayout1 = new FlowLayout();FlowLayout flowLayout2 = new FlowLayout();JButton jButton2 = new JButton();JButton jButton3 = new JButton();JButton jButton4 = new JButton();GridLayout gridLayout1 = new GridLayout();JScrollPane jScrollPane1 = new JScrollPane();JTable jTable1 = new JTable();public Frame1() {try {jbInit();}catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {Frame1 frame1 = new Frame1();frame1.setSize(new Dimension(400, 350));frame1.show();}private void jbInit() throws Exception {this.getContentPane().setLayout(borderLayout1);jPanel1.setBackground(Color.red);jPanel1.setLayout(flowLayout1);jPanel2.setBackground(Color.red);jPanel2.setLayout(flowLayout2);jPanel3.setBackground(Color.pink);jPanel3.setLayout(gridLayout1);jLabel1.setPreferredSize(new Dimension(100, 16));jLabel1.setText("请输入查询条件");jTextField1.setPreferredSize(new Dimension(140, 22));jTextField1.setText("");jButton1.setText("查询");jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));flowLayout1.setAlignment(FlowLayout.LEFT);flowLayout1.setHgap(5);flowLayout1.setVgap(10);jButton2.setText("增加");jButton3.setText("删除");jButton4.setText("修改");flowLayout2.setHgap(30);flowLayout2.setVgap(5);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.getContentPane().add(jPanel1, BorderLayout.NORTH);jPanel1.add(jLabel1, null);jPanel1.add(jTextField1, null);jPanel1.add(jButton1, null);this.getContentPane().add(jPanel2, BorderLayout.SOUTH);jPanel2.add(jButton2, null);jPanel2.add(jButton3, null);jPanel2.add(jButton4, null);this.getContentPane().add(jPanel3, BorderLayout.CENTER);jPanel3.add(jScrollPane1, null);jScrollPane1.getViewport().add(jTable1, null);}//模拟查询数据库void jButton1_actionPerformed(ActionEvent e) {try { //制作表Vector vcol = new Vector(); //列名Vector vrow = new Vector(); //内容for (int col = 1; col < 31; col ) {vcol.addElement("列" col);}for (int row = 1; row < 101; row ) {Vector vr1 = new Vector();for (int col = 1; col < 31; col ) {vr1.addElement(row "/" col);}vrow.addElement(vr1);}DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);jTable1 = new JTable(vrow, vcol);jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //滚动条设置左右滚this.jScrollPane1.getViewport().add(jTable1, null); //在滚动条中放入表}catch (Exception ex) {JOptionPane.showMessageDialog(null, ex);}}}class Frame1_jButton1_actionAdapterimplements java.awt.event.ActionListener {Frame1 adaptee;Frame1_jButton1_actionAdapter(Frame1 adaptee) {this.adaptee = adaptee;}public void actionPerformed(ActionEvent e) {adaptee.jButton1_actionPerformed(e);}}
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 科加斯暴龙皮肤 昂科威加92还是加95 五十凯 五十开扑克的玩法 五十铃k600 5十k 国产五十一自产区 五十一号元素 五十一区 第五十一号元素 美国五十一区 五十一蹬长城旅游 自拍五十一页 五十一 元素周期表五十一号元素 第五十一章吃掉陈圆圆 五十万车 五十万买什么车 贷款五十万 五十万左右的跑车 公积金贷款五十万 五十万的车有哪些 五十万 五十万买什么suv车好 五十元以下的手机 大写五十元怎么写 奎屯五十元一条街 民国五十铜元价格 五十块钱图片 五十块作品 我的房间有扇任意门五十块 从地狱归来的男子 五十块 我的房间有扇任意门 五十块 五十岚 五十岚纪子 五十岚忍 五十岚星兰 友人母五十岚在线 五十岚唱乃 台湾五十岚奶茶 五十岚虎