简体   繁体   English

GridBagLayout内容未对齐JPanel的右侧

[英]GridBagLayout content not aligning to the right of JPanel

I have a JFrame (BorderLayout) holding a JPanel(GridBagLayout) in the South position. 我有一个JFrame(BorderLayout)在南部位置持有一个JPanel(GridBagLayout)。 The JPanel border fills the screen horizontally (as I wanted it to), and so does the content within it (I don't want that). JPanel边框水平填满屏幕(如我所愿),其中的内容也填满(我不想要)。

It's much easier to visualize, so I did in Photoshop what I couldn't figure out in Java... 可视化要容易得多,所以我在Photoshop中做了用Java无法理解的工作...

I made this in photoshop to demonstrate what I WANT to happen: 我在photoshop中进行了此演示,以演示我想要发生的事情: 所需的布局 This is what my code produces: 这是我的代码产生的: 我得到什么

Here's the code I'm using: 这是我正在使用的代码:

private void loadTags(String filePath)
{
    Map<String, ArrayList<String>> fileTags;

    try
    {
        fileTags = PowerPointManipulator.getTagsFromFile(filePath);
    }
    catch (IOException e)
    {
        System.err.println("Could not open Powerpoint File");
        e.printStackTrace();
        return;
    }

    tag_listener = new TagButtonListener();

    pnl_tags = new JPanel();
    pnl_tags.setBackground(new Color(0, 0, 0, 0));
    pnl_tags.setLayout(new GridBagLayout());
    pnl_tags.setAlignmentX(LEFT_ALIGNMENT);

    brd_tags = new TitledBorder("Tags");
    brd_tags.setTitleColor(Color.WHITE);
    brd_tags.setBorder(new LineBorder(Color.WHITE));
    pnl_tags.setBorder(brd_tags);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.insets = new Insets(0, 0, 2, 15);
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    int col = 0;

    for (String key : fileTags.keySet())
    {
        ArrayList<String> vals = fileTags.get(key);
        gbc.gridwidth = 2;
        gbc.gridx = col;
        gbc.gridy = 0;

        JToggleButton tempButton = new JToggleButton(key);
        tempButton.setOpaque(false);
        tempButton.addActionListener(tag_listener);
        tempButton.setFocusable(false);

        pnl_tags.add(tempButton, gbc);

        int row = 1;

        for (String val : vals)
        {
            tempButton = new JToggleButton(val);
            tempButton.setOpaque(false);
            tempButton.addActionListener(tag_listener);
            tempButton.setFocusable(false);

            gbc.gridwidth = 1;
            gbc.gridy = row;
            pnl_tags.add(tempButton, gbc);

            row++;
        }

        col += 2;
    }

    contentPane.add(pnl_tags, BorderLayout.PAGE_END);
}

If I remove the "weight" options, then I get the proper layout, except that the buttons are centered within the JPanel. 如果删除“权重”选项,那么我将获得正确的布局,除了按钮在JPanel中居中。

I feel like I'm so close, but I can't get the exact right settings! 我觉得我已经很近了,但是我无法获得正确的正确设置! Any help is much appreciated! 任何帮助深表感谢!

GridBagConstraints#weightx and GridBagConstraints#weighty will cause the component to occupy all the remaining space left over after all the other components have been laid out, GridBagConstraints#fill will cause the component to fill the available space of the cell it resides in based on the value you supply so, GridBagConstraints#weightxGridBagConstraints#weighty将导致组件占据所有其他组件的布局后剩余的所有剩余空间, GridBagConstraints#fill将使组件填充其所驻留的单元格的可用空间您提供的价值,

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;

is doing exactly what you asked it to. 完全按照您的要求进行。

You could try something like... 您可以尝试类似...

List<String> tags = new ArrayList<>(25);
tags.add("example");
tags.add("objective");
tags.add("motivation");
tags.add("summary");
tags.add("c");
tags.add("*");
tags.add("*");
tags.add("*");
tags.add("cs");

JPanel tagPane = new JPanel(new GridBagLayout());
tagPane.setBorder(new TitledBorder("Tags"));

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
for (String tag : tags) {

    tagPane.add(new JToggleButton(tag), gbc);
    gbc.gridx--;
    if (gbc.gridx < 0) {
        gbc.gridx = 3;
        gbc.gridy++;
    }

}

Which results in something like... 导致类似...

几乎

Okay, so that's a little better, but they are grouped in the center! 好吧,这样会更好一些,但是它们被分组在中间!

Well, you could set it so each right hand side column has a weightx of 1 , for example... 好吧,您可以设置它,以便每个右侧列的weightx1 ,例如...

gbc.gridx--;
if (gbc.gridx < 0) {
    gbc.gridx = 3;
    gbc.gridy++;
    gbc.weightx = 1;
} else {
    gbc.weightx = 0;
}

Or add a "filler" component to the right of all the other components... 或在所有其他组件的右侧添加“填充器”组件。

for (String tag : tags) {

    tagPane.add(new JToggleButton(tag), gbc);
    gbc.gridx--;
    if (gbc.gridx < 0) {
        gbc.gridx = 3;
        gbc.gridy++;
    }

}
JLabel filler = new JLabel();
gbc.gridx = 4;
gbc.gridy = 0;
gbc.weightx = 1;
tagPane.add(filler, gbc);

Either way, you end up with something like.... 无论哪种方式,您最终都会得到类似...的信息。

完成

Take a closer look at How to Use GridBagLayout for more details 详细了解如何使用GridBagLayout

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

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