如何设置JTextField的大小呢?

最近用swing做了一个界面,说真的,swing的布局,外观太不好调了。
请问谁能告诉我如下将下面的textfild的大小调成跟光标那么高的高度吗?
[img]http://dl.iteye.com/upload/attachment/248398/8cfbd67e-8461-3563-bff1-87a91db92255.jpg[/img]

是性别单选按键组的问题,你性别单选按钮组是用的JPanel装的两个JRadioButton吧,把这个JPanel的Layout设为 new FlowLayout(FlowLayout.CENTER, 0, 0)就OK了,见下面代码:
[code="java"]
package org.zergle.test.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class ReaderAddIFrame extends JFrame {
private JLabel lblName = new JLabel("姓名");
private JTextField txtName = new JTextField();
private JLabel lblSex = new JLabel("性别");
private JRadioButton radMale = new JRadioButton("男");
private JRadioButton radFemale = new JRadioButton("女");
private JLabel lblAge = new JLabel("年齡");
private JTextField txtAge = new JTextField();
private JLabel lblCareer = new JLabel("职业");
private JTextField txtCareer = new JTextField();

private JPanel pnl1 = new JPanel();
private JPanel pnl1_1 = new JPanel(new GridLayout(2, 4));
private JPanel pnlSexGroup = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

public ReaderAddIFrame() {
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.pnlSexGroup.add(this.radMale);
    this.pnlSexGroup.add(this.radFemale);
    this.pnl1_1.add(this.lblName);
    this.pnl1_1.add(this.txtName);
    this.pnl1_1.add(this.lblSex);
    this.pnl1_1.add(this.pnlSexGroup);
    this.pnl1_1.add(this.lblAge);
    this.pnl1_1.add(this.txtAge);
    this.pnl1_1.add(this.lblCareer);
    this.pnl1_1.add(this.txtCareer);
    this.pnl1.add(this.pnl1_1);
    this.add(this.pnl1, BorderLayout.CENTER);
    this.setSize(600, 400);
    this.setVisible(true);
}

public static void main(String[] args) {
    new ReaderAddIFrame();
}

}
[/code]

setSize(宽度,高度)

试试

如果父容器使用了布局管理器,则使用setPreferredSize(). 对于一些会调整组件大小的布局管理器(例如BorderLayout)来说,无法精确设置组件的大小.
如果父容器未使用布局管理器,使用setSize()方法.

你是用的BorderLayout 套的 GridLayout 的吧,这样会导致控件被拉伸,建议这样嵌套:

BorderLayout -> FlowLayout(ALIGN_CENTER) -> GridLayout

把代码粘出来看看

我是看了GridLayout的源码才知道这个问题:
[code="java"]
/**
* Determines the preferred size of the container argument using
* this grid layout.
*


* The preferred width of a grid layout is the largest preferred
* width of all of the components in the container times the number of
* columns, plus the horizontal padding times the number of columns
* minus one, plus the left and right insets of the target container.
*


* The preferred height of a grid layout is the largest preferred
* height of all of the components in the container times the number of
* rows, plus the vertical padding times the number of rows minus one,
* plus the top and bottom insets of the target container.
*
* @param parent the container in which to do the layout
* @return the preferred dimensions to lay out the
* subcomponents of the specified container
* @see java.awt.GridLayout#minimumLayoutSize
* @see java.awt.Container#getPreferredSize()
*/
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = rows;
int ncols = cols;

if (nrows > 0) {
    ncols = (ncomponents + nrows - 1) / nrows;
} else {
    nrows = (ncomponents + ncols - 1) / ncols;
}
int w = 0;
int h = 0;
for (int i = 0 ; i < ncomponents ; i++) {
    Component comp = parent.getComponent(i);
    Dimension d = comp.getPreferredSize();
    if (w < d.width) {
    w = d.width;
    }
    if (h < d.height) {
    h = d.height;
    }
}
return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
             insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  }
}[/code]

在GridLayout中,每个Component的宽度是以最宽的那个组件为基准,高度是以最高的那个组件为基准。而FlowLayout默认有个vgap = 5, hgap = 5。这样就把面板撑大了。