如题,控件有Label,textbox,table等。

解决方案 »

  1.   

    在VB中常有这样的要求,一般是对窗体中的控件进行遍历,但Java中这样做不行。如果把所有的控件放到一个数组中也可以,就是比较麻烦。
      

  2.   

    jgoodies的布局控件! 绝对好用+免费!
      

  3.   

    谢谢大家的提醒。我开始时用的是baggridlayout,但使用过程中效果不好,也许是我没有用好的原因,现在我采用了 ComponentListener 接口的办法,在componentResized中进行处理,代码如下:public class NtpInfoPanel extends JPanel implements ComponentListener {
    private JLabel ipLabel = new JLabel("NTP Server Information");
    private JTextField ipTextField = new JTextField("", 15);
    private JLabel samplingLabel = new JLabel("Sampling Times");
    private JTextField samplingTextField = new JTextField("", 15);
    private JLabel samplingUnitLabel = new JLabel("sec");
    private JButton getButton = new JButton("Get");
    private JButton putButton = new JButton("Put"); public NtpInfoPanel() {
    super();
    setStatus();
    setComponent();
    } private void setStatus() {
    // Set layout
    this.setLayout(new GridLayout(1, 1)); // Set Border
    setBorder(
    BorderFactory.createCompoundBorder(
    BorderFactory.createTitledBorder("NTP Information"),
    BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    } private void setComponent() {
    setLayout(null); add(ipLabel);
    add(ipTextField);
    add(samplingLabel);
    add(samplingTextField);
    add(samplingUnitLabel);
    add(getButton);
    add(putButton); addComponentListener(this);
    } public void componentMoved(ComponentEvent e) {
    } public void componentHidden(ComponentEvent e) {
    }

    public void componentShown(ComponentEvent e) {
    } public void componentResized(ComponentEvent e) {
    Component c = e.getComponent();
    int offsetValue=20;
    int nWidth=c.getWidth()-2*offsetValue;
    int nHeight=c.getHeight()-2*offsetValue;

    int nIntevalX=nWidth/7;
    int nInnerWidth=nIntevalX-10;
    int nIntevalY=nHeight;

    int nPosX=offsetValue;
    ipLabel.setLocation(nPosX, offsetValue);
    ipLabel.setSize(nInnerWidth, nIntevalY);

    nPosX+=nIntevalX;
    ipTextField.setLocation(nPosX, offsetValue);
    ipTextField.setSize(nInnerWidth, nIntevalY);

    nPosX+=nIntevalX;
    samplingLabel.setLocation(nPosX+10, offsetValue);
    samplingLabel.setSize(nInnerWidth, nIntevalY);

    nPosX+=nIntevalX;
    samplingTextField.setLocation(nPosX, offsetValue);
    samplingTextField.setSize(nInnerWidth, nIntevalY);

    nPosX+=nIntevalX;
    samplingUnitLabel.setLocation(nPosX, offsetValue);
    samplingUnitLabel.setSize(nInnerWidth, nIntevalY);

    nPosX+=nIntevalX;
    getButton.setLocation(nPosX, offsetValue);
    getButton.setSize(nInnerWidth, nIntevalY);

    nPosX+=nIntevalX;
    putButton.setLocation(nPosX, offsetValue);
    putButton.setSize(nInnerWidth, nIntevalY);
    }
    }