2

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

在此处输入图像描述

以下是我目前用来制作滚动窗格和相关组件的代码。

 //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中所指出的,但它似乎根本不起作用。

4

5 回答 5

3

您只是忘记提供任何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();
            }
        });
    }
}
于 2012-05-22T09:54:51.733 回答
2

尝试使用BorderLayoutcontrols.setLayout(new BorderLayout());然后将其应用于您的 JPanelcontrols.add(yourPanel, BorderLayout.PAGE_START);

我也有问题,GridBagLayout所以我解决了它,BorderLayout它工作得很好。


所以我为你的小例子写了:

private void initComponents() {

        controls = new Container();
        controls = getContentPane();
        controls.setLayout(new BorderLayout());

        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        field = new JTextField(20);
        c.gridx = 0;
        c.gridy = 0;
        panel.add(field, c);

        one = new JButton("Go!");
        c.gridx = 1;
        c.gridy = 0;
        panel.add(one, c);

        controls.add(panel, BorderLayout.PAGE_START);

    }  

希望能帮助到你!

于 2012-05-22T09:31:18.077 回答
2

抱歉,我的回答与您的要求不符,但是您为什么不使用 GroupLayout 而不是 GridBag Layout,这样更容易处理

于 2012-05-22T09:32:17.810 回答
2

我认为这可能是简单和可能的,你可以

对此JPanel

  • JPanels包含JComponentGridLayout(关于滚动的通知,您必须更改滚动增量)

或使用最复杂JComponents

  • JPanels包含JComponent作为项目添加到JList

  • JPanels包含JComponent作为JTable的行(只有一列,有或没有TableHeader

于 2012-05-22T09:43:13.113 回答
0

在右侧添加一个面板,在底部添加一个面板。

右面板: 将权重 X设置为1.0。将填充设置为水平

底部面板:权重 Y设置为1.0。将填充设置为垂直

可能有更好的方法,但这对我有用。

于 2017-03-22T07:27:53.833 回答