当前位置: 首页 > 知识库问答 >
问题:

GridBagLayout单元格取整行

庾奇思
2023-03-14

我正在制作一个小型java swing应用程序,我必须在ScrollPane中创建一个带有gridBagLayout的表。

首先,GridBagLayout没有在面板的顶部对齐。

第二,如果我将(1,2,3,4)单元格居中,则它们居中在面板的中间(而不是单元格的中间)。

这是我的代码:

//Folder --------------------------------------------------------
    folderPanel.setBackground(Color.WHITE);
    folderPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.NORTHWEST;

    JLabel iCell = new JLabel();
    JLabel nameCell = new JLabel();
    JLabel typeCell = new JLabel();
    JLabel dateCell = new JLabel();
    iCell.setPreferredSize(new Dimension(50, 60));
    iCell.setHorizontalAlignment(SwingConstants.CENTER);
    nameCell.setPreferredSize(new Dimension(50, 60));
    nameCell.setHorizontalAlignment(SwingConstants.CENTER);
    typeCell.setPreferredSize(new Dimension(50, 60));
    typeCell.setHorizontalAlignment(SwingConstants.CENTER);
    dateCell.setPreferredSize(new Dimension(50, 60));
    dateCell.setHorizontalAlignment(SwingConstants.CENTER);
    int gridy = 0;
    int i = 0;

    iCell.setText("#");
    gbc.gridx = 0;
    gbc.gridy = gridy;
    folderPanel.add(iCell, gbc);
    //---------------------------------------------
    nameCell.setText("Name");
    gbc.gridx = 1;
    gbc.gridy = gridy;
    folderPanel.add(nameCell, gbc);
    //---------------------------------------------
    typeCell.setText("Type");
    gbc.gridx = 2;      
    gbc.gridy = gridy;
    folderPanel.add(typeCell, gbc);     
    //---------------------------------------------
    dateCell.setText("Date");
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridx = 3;  
    gbc.gridy = gridy;
    folderPanel.add(dateCell, gbc);

    gridy++;
    i++;

    DBCursor cursor = imagesCollection.find();
    try {
        while(cursor.hasNext()) {
            DBObject obj = cursor.next();

            iCell = new JLabel();
            nameCell = new JLabel();
            typeCell = new JLabel();
            dateCell = new JLabel();
            iCell.setPreferredSize(new Dimension(50, 60));
            nameCell.setPreferredSize(new Dimension(50, 60));
            typeCell.setPreferredSize(new Dimension(50, 60));
            dateCell.setPreferredSize(new Dimension(50, 60));

            iCell.setText(Integer.toString(i));
            gbc.gridx = 0;
            gbc.gridy = gridy;
            folderPanel.add(iCell, gbc);
            //---------------------------------------------
            nameCell.setText((String) obj.get("name"));
            gbc.gridx = 1;
            gbc.gridy = gridy;
            folderPanel.add(nameCell, gbc);
            //---------------------------------------------
            typeCell.setText((String) obj.get("type"));
            gbc.gridx = 2;      
            gbc.gridy = gridy;
            folderPanel.add(typeCell, gbc);     
            //---------------------------------------------
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            dateCell.setText(df.format(obj.get("creationDate")));
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.gridx = 3;  
            gbc.gridy = gridy;
            folderPanel.add(dateCell, gbc);

            gridy++;
            i++;
        }

    } finally {
       cursor.close();
    }
    JScrollPane scroller = new JScrollPane(folderPanel);

共有1个答案

赵夕
2023-03-14
//Folder --------------------------------------------------------
folderPanel.setBackground(Color.WHITE);
folderPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 0;    /////////////////////////////   added
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTHWEST;

JLabel iCell = new JLabel();
JLabel nameCell = new JLabel();
JLabel typeCell = new JLabel();
JLabel dateCell = new JLabel();
iCell.setPreferredSize(new Dimension(50, 60));
iCell.setHorizontalAlignment(SwingConstants.CENTER);
nameCell.setPreferredSize(new Dimension(50, 60));
nameCell.setHorizontalAlignment(SwingConstants.CENTER);
typeCell.setPreferredSize(new Dimension(50, 60));
typeCell.setHorizontalAlignment(SwingConstants.CENTER);
dateCell.setPreferredSize(new Dimension(50, 60));
dateCell.setHorizontalAlignment(SwingConstants.CENTER);
int gridy = 0;
int i = 0;

iCell.setText("#");
gbc.gridx = 0;
gbc.gridy = gridy;
folderPanel.add(iCell, gbc);
//---------------------------------------------
nameCell.setText("Name");
gbc.gridx = 1;
gbc.gridy = gridy;
folderPanel.add(nameCell, gbc);
//---------------------------------------------
typeCell.setText("Type");
gbc.gridx = 2;      
gbc.gridy = gridy;
folderPanel.add(typeCell, gbc);     
//---------------------------------------------
dateCell.setText("Date");
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 3;  
gbc.gridy = gridy;
folderPanel.add(dateCell, gbc);

gbc.weighty = 1;        ////////////////////////   added
gridy++;
i++;

DBCursor cursor = imagesCollection.find();
try {
    while(cursor.hasNext()) {
        DBObject obj = cursor.next();

        iCell = new JLabel();
        nameCell = new JLabel();
        typeCell = new JLabel();
        dateCell = new JLabel();
        iCell.setPreferredSize(new Dimension(50, 60));
        nameCell.setPreferredSize(new Dimension(50, 60));
        typeCell.setPreferredSize(new Dimension(50, 60));
        dateCell.setPreferredSize(new Dimension(50, 60));

        iCell.setText(Integer.toString(i));
        gbc.gridx = 0;
        gbc.gridy = gridy;
        folderPanel.add(iCell, gbc);
        //---------------------------------------------
        nameCell.setText((String) obj.get("name"));
        gbc.gridx = 1;
        gbc.gridy = gridy;
        folderPanel.add(nameCell, gbc);
        //---------------------------------------------
        typeCell.setText((String) obj.get("type"));
        gbc.gridx = 2;      
        gbc.gridy = gridy;
        folderPanel.add(typeCell, gbc);     
        //---------------------------------------------
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        dateCell.setText(df.format(obj.get("creationDate")));
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridx = 3;  
        gbc.gridy = gridy;
        folderPanel.add(dateCell, gbc);

        gridy++;
        i++;
    }

} finally {
   cursor.close();
}
JScrollPane scroller = new JScrollPane(folderPanel);
 类似资料:
  • 我想要的是让第一个textField更靠近第一个jlabel,在第一个textField和第二个jlabel之间有一些空间,像这样: JLabel:JTEXTFIELD-----(spaceeee)-------JLabel:JTEXTFIELD 但我所改变的一切都不能接近我想要的,你看:

  • 我试图根据任意单元格的值,获取从E到BH的特定范围内的整行数据。例如,如果单元格P,3上的值为“红色”,则我希望选择从E3到BH,3的整个范围。 此外,如果F9中的单元格上有值“黄色”,我希望选择从E9到BH9的整行 到目前为止,我一直在尝试从字符串“my text”中获取行,但它不起作用。我计划以与类似的方式获取行和列,以便根据文本获取从e(行)到bh(行)的行

  • 在具有1列和多行的面板中使用方框布局求解。

  • 1、我希望能够从封闭工作簿的单元格中读取值 算法: > 从关闭的excel文件读取- 需要读取第一行第1列到第200列的整行 变量1等于(A1)的单元格值,其中A1是字符串变量2等于(B1)的单元格值,其中B1是整数变量3等于(c1)的单元格值,其中c1是字符串:::变量200等于(第1行第200列)的单元格值,其中值是字符串 最好的方法是什么? 我应该能穿过100排 你能告诉我吗

  • 问题内容: 我已经看过这个问题并在Google上搜索了一下,但到目前为止没有任何效果。我认为现在是2010年(这些问题/答案很旧,而且还没有答案),我们有了CSS3!有什么方法可以使用CSS使div填充整个表格单元格的宽度和高度吗? 我不知道单元格的宽度和/或高度会提前多少,并且将div的宽度和高度设置为100%无效。 另外,我需要div的原因是因为我需要将某些元素绝对定位在单元格之外,并且不适用

  • 问题内容: 我正在将AutoSizing单元格与Autolayout和UICollectionView一起使用。 我可以在单元初始化的代码中指定约束: 但是,由于该单元尚未添加到中,该应用程序崩溃了。 问题 在该阶段的生命周期,可以添加一个约束的? 有没有作出任何默认方式的equal to the of thewithout accessing an instance of or? 编辑 这个问题