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()
}
}
问题就在于你没有为每个item指定宽高
之前那句
bn.setLayoutParams(lp)实际上是无效的,因为btn的父控件是GridItem而不是RelativeLayout
修改的话,在
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, columnSpec)这句后面加上设置宽高的语句
//宽高实际数值你自己计算,这里取40params.width = 40
params.height = 40
这样就行了,顺便附上改后真机上的效果
GridLayout是网格布局,它会均分你的布局容器,所以行列高度和宽度是统一的,只能通过调整布局容器大小来改变。如果要控制单元网格的大小,那只能用GridBagLayout了
关于设置列宽和行高,GridView本身只是个视图展示和listview一样,要想把里面的元素缩小放大,把添加到里面的布局缩小放大即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)