简体   繁体   English

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

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

I have a Swing Form that contains a JScrollPane( activityScrollPane ) for a JPanel( activityPanel ). 我有一个包含一个JScrollPane(Swing的表格activityScrollPane为一个JPanel() activityPanel )。 The panel contains a JTextField and a JButton (that is used to add more fields to the Panel). 该面板包含一个JTextField和一个JButton(用于向Panel添加更多字段)。 Now the problem is that the elements start from the center of the panel as in the image below (with the borders marking the activityScrollPane boundary) 现在问题是元素从面板中心开始,如下图所示(边框标记activityScrollPane边界)

在此输入图像描述

Following is the code I am currently using to make the scroll pane and associated components. 以下是我目前用于制作滚动窗格和相关组件的代码。

 //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);

How can I make the JTextField and the JButton or for that matter any component to appear on the top left corner of the JScrollPane. 如何使JTextField和JButton或者任何组件出现在JScrollPane的左上角。 I have already tried using 我已经尝试过使用了

activityConstraints.anchor = GridBagConstraints.NORTHWEST;

as pointed in the SO post , but it does not at all seem to work. 正如SO 帖子所指出的那样,但它似乎根本不起作用。

You simply forgot to provide any weightx/weighty values, atleast one having a non-zero value will do. 你只是忘了提供任何weightx/weighty值,至少有一个非零值的值。 have a look at this code example : 看看这个代码示例:

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();
            }
        });
    }
}

Latest EDIT : No spacing along Y-Axis 最新编辑:沿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();
            }
        });
    }
}

try it with BorderLayout : controls.setLayout(new BorderLayout()); 尝试使用BorderLayoutcontrols.setLayout(new BorderLayout()); and then apply it for your JPanel controls.add(yourPanel, BorderLayout.PAGE_START); 然后将其应用于JPanel controls.add(yourPanel, BorderLayout.PAGE_START);

I also have problems with GridBagLayout so i solved it with BorderLayout and it works so fine. 我也有GridBagLayout问题,所以我用BorderLayout解决了它,它工作得很好。


So i wrote for your little example: 所以我写了你的小例子:

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);

    }  

Hope it helps! 希望能帮助到你!

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

I think this could be simple and possible, you can 我认为这可能很简单,也可以

to this JPanel 这个JPanel

  • put JPanels contains JComponent to the GridLayout (notice about scrolling, you have to change scrolling increment) JPanels包含JComponentGridLayout (关于滚动的注意事项,你必须改变滚动增量)

or use most complex JComponents as 或者使用最复杂的JComponents

  • put JPanels contains JComponent as Item to the JList JPanels包含JComponent作为Item给JList

  • put JPanels contains JComponent as row to the JTable (with only one Column, with or without TableHeader ) JPanels包含JComponent作为行添加到JTable (只有一个列,有或没有TableHeader

Add one panel at the right and one at the bottom. 在右侧添加一个面板,在底部添加一个面板。

Right Panel: Set Weight X to 1.0 . 右侧面板:重量X设置为1.0 Set Fill to horizontal . 填充设置为水平

Bottom Panel: Set Weight Y to 1.0 . 底部面板:重量Y设置为1.0 Set Fill to vertical 填充设置为垂直

There may be better ways to that, but this one worked for me. 可能有更好的方法,但这个对我有用。

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

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