java - 是否可以使带有 GridBagLayout 的 JPanel 内的元素从左上角开始?

标签 java swing gridbaglayout

我有一个 Swing 表单,其中包含 JPanel(activityPanel) 的 JScrollPane(activityScrollPane)。该面板包含一个 JTextField 和一个 JButton(用于向面板添加更多字段)。现在的问题是元素从面板的中心开始,如下图所示(边框标记 activityScrollPane 边界)

enter image description here

以下是我当前用于制作滚动 Pane 和关联组件的代码。

 //part of the code for creating the ScrollPane

        final JPanel activityPanel = new JPanel(new GridBagLayout());
        gbc.gridx=0;
        gbc.gridy=0;
        JScrollPane activityScrollPane = new JScrollPane(activityPanel); 
        //adding activity fields
        activityFields = new ArrayList<JTextField>();
        fieldIndex = 0;
        activityFields.add(new JTextField(30));

        final GridBagConstraints activityGBC = new GridBagConstraints();
        activityGBC.gridx=0;
        activityGBC.gridy=0;
        activityGBC.anchor = GridBagConstraints.FIRST_LINE_START;
        activityPanel.add(activityFields.get(fieldIndex),activityGBC);

        fieldIndex++;
        JButton btn_more = (new JButton("more"));
        activityGBC.gridx=1;
        activityPanel.add(btn_more,activityGBC);

如何使 JTextField 和 JButton 或任何组件出现在 JScrollPane 的左上角。我已经尝试过使用

activityConstraints.anchor = GridBagConstraints.NORTHWEST;

如 SO post 中所指出的,但它似乎根本不起作用。

最佳答案

您只是忘记提供任何 weightx/weighty 值,至少有一个非零值即可。看看这个代码示例:

import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo
{
    private JTextField tfield1;
    private JButton button1;

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        tfield1 = new JTextField(10);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;  
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;       
        contentPane.add(tfield1, gbc);

        button1 = new JButton("More");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        contentPane.add(button1, gbc);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutDemo().displayGUI();
            }
        });
    }
}

最新编辑:沿 Y 轴无间距

import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo
{
    private JTextField tfield1;
    private JButton button1;
    private JTextField tfield2;
    private JButton button2;

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        tfield1 = new JTextField(10);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
        gbc.weightx = 1.0;
        //gbc.weighty = 0.2;    
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;       
        contentPane.add(tfield1, gbc);

        button1 = new JButton("More");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        contentPane.add(button1, gbc);

        tfield2 = new JTextField(10);
        gbc.weightx = 1.0;
        gbc.weighty = 0.2;  
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;       
        contentPane.add(tfield2, gbc);

        button2 = new JButton("More");
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        contentPane.add(button2, gbc);

        JScrollPane scroller = new JScrollPane(contentPane);

        frame.add(scroller);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutDemo().displayGUI();
            }
        });
    }
}

关于java - 是否可以使带有 GridBagLayout 的 JPanel 内的元素从左上角开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10699277/

相关文章:

Java Swing Workers 和组合框问题

java - JTable 单元格跨度合并

java - 在 java 中使用 GridBagLayout 时设置最大组件大小

java - 使对话框保持在顶部

java - LibGDX 鼠标位置在世界偏差中

java - JSP 错误 -- 应为等号

java - 添加组件时 Gridbag 布局会调整大小

java - 将图像设置为 JPanel 背景的最简单方法

java - 如何隐藏 "java desktop application"

java - 创建 Gridbag 布局