主要就用到了这四个方法
createHorizontalBox()
创建一个从左到右显示其组件的 Box
。
createHorizontalStrut(int width) //左右部件之间的中间间隔就可以用这个方法来控制
创建一个不可见的、固定宽度的组件。
createVerticalBox()
创建一个从上到下显示其组件的 Box
。
createVerticalStrut(int height) //上下部件之间的中间间隔就可以用这个方法来控制
创建一个不可见的、固定高度的组件。
效果图如下
![](https://p-blog.csdn.net/images/p_blog_csdn_net/snailjava/278835/o_070326boxlayouer.gif)
package com.geogro.webapp.applet.track; import java.awt.*; import javax.swing.*; import javax.swing.border.*; /**3G版本的数据过滤面板,输入过滤值查询3G值. * * @author snail * @date 07-03-26 * @version 1.0 */ public class Fliter3GPanel extends JPanel { private static Font defaultFont = new Font("SimSun", Font.PLAIN, 12); //默认字体 TitledBorder titledBorder1 = new TitledBorder("参数设置"); JTextField xTextField; JTextField yTextField; JTextField zTextField; JTextField inteTextField; DateChooserJButton fliterStartTimeButton; DateChooserJButton fliterEndTimeButton; JProgressBar fliterLoadBar; JButton okButton; JButton cancelButton; private void jbInit() throws Exception { Box b = Box.createVerticalBox(); JLabel bannerLabel = new JLabel("3G数据过滤查询"); b.add(bannerLabel); //-------------setTheInputPanel------------------------------ JPanel inputPanel = new JPanel(); TitledBorder inputPanelBorder = new TitledBorder("设置参数"); inputPanelBorder.setTitleFont(defaultFont); inputPanel.setBorder(inputPanelBorder); Box vtemp = Box.createVerticalBox(); Box htemp1 = Box.createHorizontalBox(); Box htemp2 = Box.createHorizontalBox(); Box htemp3 = Box.createHorizontalBox(); Box htemp4 = Box.createHorizontalBox(); Box htemp5 = Box.createHorizontalBox(); Box htemp6 = Box.createHorizontalBox(); Box htemp7 = Box.createHorizontalBox(); Box htemp8 = Box.createHorizontalBox(); xTextField = new JTextField(); xTextField.setPreferredSize(new Dimension(50, 10)); htemp1.add(new JLabel("横向:")); htemp1.add(Box.createHorizontalStrut(10)); //创建label和textFied之间的距离 htemp1.add(xTextField); htemp1.add(new JLabel("(例:3.01)")); yTextField = new JTextField(); yTextField.setPreferredSize(new Dimension(50, 10)); htemp2.add(new JLabel("纵向:")); htemp2.add(Box.createHorizontalStrut(10)); htemp2.add(yTextField); htemp2.add(new JLabel("(例:4.01)")); zTextField = new JTextField(); zTextField.setPreferredSize(new Dimension(50, 10)); htemp3.add(new JLabel("垂直:")); htemp3.add(Box.createHorizontalStrut(10)); htemp3.add(zTextField); htemp3.add(new JLabel("(例:3.01)")); inteTextField = new JTextField(); inteTextField.setPreferredSize(new Dimension(50, 10)); htemp4.add(new JLabel("综合:")); htemp4.add(Box.createHorizontalStrut(10)); htemp4.add(inteTextField); htemp4.add(new JLabel("(例:5.01)")); fliterStartTimeButton = new DateChooserJButton(); JLabel tmpLabel = new JLabel("开始时间:"); htemp5.add(tmpLabel); htemp5.add(fliterStartTimeButton); fliterEndTimeButton = new DateChooserJButton(); JLabel tmpLabel2 = new JLabel("结束时间:"); htemp6.add(tmpLabel2); htemp6.add(fliterEndTimeButton); fliterLoadBar = new JProgressBar(); JLabel tmpLabel3 = new JLabel("进度:"); htemp7.add(tmpLabel3); htemp7.add(fliterLoadBar); okButton = new JButton("过滤"); cancelButton = new JButton("撤销"); htemp8.add(okButton); htemp8.add(Box.createHorizontalStrut(10)); htemp8.add(cancelButton); vtemp.add(htemp1); vtemp.add(Box.createVerticalStrut(10)); //创建上下空间距离 vtemp.add(htemp2); vtemp.add(Box.createVerticalStrut(10)); vtemp.add(htemp3); vtemp.add(Box.createVerticalStrut(10)); vtemp.add(htemp4); vtemp.add(Box.createVerticalStrut(10)); vtemp.add(htemp5); vtemp.add(Box.createVerticalStrut(10)); vtemp.add(htemp6); vtemp.add(Box.createVerticalStrut(25)); vtemp.add(htemp7); vtemp.add(Box.createVerticalStrut(5)); vtemp.add(htemp8); inputPanel.add(vtemp); //---------------------------------------------------------- //------------other panel init here------------------------- //initCode! //---------------------------------------------------------- b.add(inputPanel); //b.add(otherPanel); this.add(b, BorderLayout.NORTH); } public Fliter3GPanel() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } }
代码如下: