繁体   English   中英

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

[英]Is it possible to make the elements inside a JPanel with GridBagLayout to start from the top left?

我有一个包含一个JScrollPane(Swing的表格activityScrollPane为一个JPanel() activityPanel )。 该面板包含一个JTextField和一个JButton(用于向Panel添加更多字段)。 现在问题是元素从面板中心开始,如下图所示(边框标记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 帖子所指出的那样,但它似乎根本不起作用。

你只是忘了提供任何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();
            }
        });
    }
}

尝试使用BorderLayoutcontrols.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);

    }  

希望能帮助到你!

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

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

这个JPanel

  • JPanels包含JComponentGridLayout (关于滚动的注意事项,你必须改变滚动增量)

或者使用最复杂的JComponents

  • JPanels包含JComponent作为Item给JList

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

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

右侧面板:重量X设置为1.0 填充设置为水平

底部面板:重量Y设置为1.0 填充设置为垂直

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM