是否可以使用GridBagLayout在JPanel中的元素从左上角开始?

时间:2012-05-22 09:26:43

标签: java swing gridbaglayout

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

enter image description here

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

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

5 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

答案 1 :(得分:2)

使用 BorderLayout controls.setLayout(new BorderLayout());进行尝试,然后将其应用于您的JPanel controls.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);

    }  

希望它有所帮助!

答案 2 :(得分:2)

很抱歉,我回答你的问题,但是为什么不使用GroupLayout代替GridBag Layout,这样更容易处理

答案 3 :(得分:2)

我认为这可能很简单,也可以,

到此JPanel

  • JPanels包含JComponent添加到GridLayout(关于滚动的注意事项,您必须更改滚动增量)

或使用最复杂的JComponents作为

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

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

答案 4 :(得分:0)

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

右侧小组:权重X 设置为 1.0 。 将填充设置为水平

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

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