caozhg
2015-05-13 03:20
采纳率: 100%
浏览 2.9k
已采纳

java GridBagLayout 表格内容不显示,,而且会自动居中

  1. 图片说明 如果不用JScrollPane,布局正常,但是不显示表头 ``` package com.haiersoft.jxc.kcgl.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class KuCunPanDianPanel extends JPanel{
private JTextField pandianyuan;
private JTextField pandianshijian;
private JTextField pinzhongshu;

//private DefaultTableModel model = new DefaultTableModel();
//private JTable table = new JTable(model);
private JScrollPane sp_table;

public KuCunPanDianPanel(){
    super();
    setBounds(10, 10, 460, 300);
    setLayout(new GridBagLayout());
    setVisible(true);
    setupComponet(new JLabel(" 盘 点 员:"), 0, 0, 1, 0, false);
    pandianyuan = new JTextField("Tsoft");
    // 定位盘点员文本框
    setupComponet(pandianyuan, 1, 0, 1, 100, true);
    setupComponet(new JLabel(" 盘点时间:"), 2, 0, 1, 0, false);
    pandianshijian = new JTextField("2010-5-18 16:40:41");
    // ·定位盘点时间文本框
    setupComponet(pandianshijian, 3, 0, 1, 150, true);
    setupComponet(new JLabel(" 品 种 数:"), 4, 0, 1, 0, false);
    pinzhongshu = new JTextField("1");
    // 定位品种数文本框
    setupComponet(pinzhongshu, 5, 0, 1, 100, true);

    String[] name={"商品名称","商品编号","供应商","产地","单位","规格","单价","数量","包装"};
    //String[] columnNames = {"Product","Number of Boxes","Price"};
    //Object data[][] = {{ "小芳", "142321" },{ "小芳", "142321" } };
    Object[][] data =
        {           
                 {"计算机", "sp1001","小公司","山东青岛","台","",new Double(10000.000), new Integer(10),"箱"},           
        };
    JTable table = new JTable(data,name);
    //table.setPreferredScrollableViewportSize(new Dimension(300, 80));
  //创建一个滚动面板,包含表格
    sp_table = new JScrollPane(table);
    setupComponet(sp_table, 0, 1, 10, 400, true);




}
private void setupComponet(JComponent component, int gridx, int gridy,
        int gridwidth, int ipadx, boolean fill) {
    final GridBagConstraints gridBagConstrains = new GridBagConstraints();
    gridBagConstrains.gridx = gridx;
    gridBagConstrains.gridy = gridy;
    gridBagConstrains.insets = new Insets(5, 1, 3, 1);
    if (gridwidth > 1)
        gridBagConstrains.gridwidth = gridwidth;
    if (ipadx > 0)
        gridBagConstrains.ipadx = ipadx;
    if (fill)
        gridBagConstrains.fill = GridBagConstraints.HORIZONTAL;
    add(component, gridBagConstrains);
}

public void initComboBox() {

}

}


1条回答 默认 最新

相关推荐 更多相似问题