盒式布局栗子





package com.cissst;大笑



import java.awt.BorderLayout;
import java.awt.GridBagConstraints;


import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;


public class box1 extends JFrame {
Box middlebox=Box.createHorizontalBox();
Box leftbox=Box.createHorizontalBox();
Box centerbox=Box.createVerticalBox();
JButton bt3=new JButton("<<");
JButton bt4=new JButton(">>");
Box rightbox=Box.createHorizontalBox();
JLabel lab1=new JLabel("运动会项目");
JTextArea area1=new JTextArea(30,45);
JButton bt1=new JButton("查询");
JButton bt2=new JButton("退出");
Box bottompanel=Box.createVerticalBox();//垂直布局
Box buttonpanel=Box.createHorizontalBox();



  
public box1()
{super ("box1");
this.setVisible(true);
this.setSize(400, 300);
this.setLocation(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 
String[] columnName = { "姓名", "性别", "单位", "参加项目", "备注" }; 
String[][] rowData = { { "张三", "男", "计算机系", "100 米 ,200 米", "" }, 
{ "李四", "男", "化学系", "100 米,铅球", "" }, 
}; 
Box topPanel=Box.createVerticalBox();
           // 创建表格
JTable table = new JTable(new DefaultTableModel(rowData, columnName)); 
           // 创建包含表格的滚动窗格
JScrollPane scrollPane = new JScrollPane(table);
//scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
         
 
           // 先加入一个不可见的 Strut,从而使 topPanel 对顶部留出一定的空间
topPanel.add(Box.createVerticalStrut(10)); 
           // 加入包含表格的滚动窗格 
topPanel.add(scrollPane); 
           // 再加入一个不可见的 Strut,从而使 topPanel 和 middlePanel 之间留出一定的空间
 topPanel.add(Box.createVerticalStrut(10)); 
leftbox.add(Box.createHorizontalStrut(20));
leftbox.add(lab1);
leftbox.add(area1);
 
leftbox.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));//设border
middlebox.add(leftbox);
leftbox.add(Box.createHorizontalStrut(20));
centerbox.add(Box.createVerticalStrut(30));
centerbox.add(bt3);
centerbox.add(Box.createVerticalGlue());
centerbox.add(bt4);
centerbox.add(Box.createVerticalStrut(30));


middlebox.add(centerbox);
middlebox.add(Box.createHorizontalStrut(20));
bottompanel.add(Box.createVerticalStrut(10));//若要增加横向布局若要增加上下间距则在最外面的布局上加struct
buttonpanel.add(bt1);
buttonpanel.add(Box.createHorizontalGlue());
buttonpanel.add(bt2);

bottompanel.add(buttonpanel);
Box box=Box.createVerticalBox();
box.add(topPanel);
box.add(middlebox);
  box.add(bottompanel);
this.add(box);





}
public static void main(String[] args) {
// TODO Auto-generated method stub
      new box1();
}


}


1.盒式布局有....X,....Y的出现,现在一般用Box.createHorizontalBox();表现,,,,

2要形成横盒子要行间距,在盒子(外层容器上,而不是在里面的组件上加)上加strut 

3.Box.createVerticalGlue());是使得里面的组件尽可能靠两边,,然后用Box.createVerticalStrut设固定间距的,,

glue很绝对,往往用strut调和

leftbox.add(Box.createHorizontalStrut(20));
centerbox.add(Box.createVerticalStrut(30));
centerbox.add(bt3);
centerbox.add(Box.createVerticalGlue());
centerbox.add(bt4);
centerbox.add(Box.createVerticalStrut(30));

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值