主要就用到了這四個方法
createHorizontalBox()
建立一個從左至右顯示其組件的 Box
。
createHorizontalStrut(int width) //左右組件之間的中間間隔就可以用這個方法來控制
建立一個不可見的、固定寬度的組件。
createVerticalBox()
建立一個從上到下顯示其組件的 Box
。
createVerticalStrut(int height) //上下組件之間的中間間隔就可以用這個方法來控制
建立一個不可見的、固定高度的組件。
如下
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(); } }}
代碼如下: