如何将 GUI 对象放置在 GridBagLayout 单元格内的特定 x/y 位置

How to place GUI object at specific x/y place within cell within GridBagLayout

我是 Java Swing 的新手,正在做一个小项目来帮助我熟悉它的使用。我正在尝试指定我的对象的放置尺寸(及其大小),但我似乎无法正确设置。我的对象大小显示正确,但无法将它们放在我想要的位置。下面是它当前的样子的示例,以及我正在寻找的内容...

目前...

需要让它看起来像...

下面是我敲出来的代码...

package Main;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang3.ArrayUtils;

public class StartGui extends JFrame implements ActionListener {

    private static final String[] GuiCharSelDefault = {"---  Select Character ---"};
    private static final int unselectedDefaultElement = 0;
    private static final String[] GuiCharSel = (String[])ArrayUtils.addAll(GuiCharSelDefault, Calculator.Characters);
    private String[] lvlRange = SupportMethods.createArrRange("- -", 1, 99);

    /*
     * Interactive GUI Objects
     */
    JLabel charPic;
    JComboBox charSelCombo = new JComboBox(GuiCharSel);
    JComboBox pickLvlAns = new JComboBox(lvlRange);
    JLabel nextLvlAns = new JLabel("- -");


    public StartGui() {
        /*
         * Non-Interactive GUI Objects
        */
        JPanel topFrame = new JPanel();
        JPanel bottomFrame = new JPanel();
        JPanel selPane = new JPanel();
        JLabel pickLvl = new JLabel("Pick Current Level:");
        JLabel nextLvl = new JLabel("Next Level:");

        //*******************************************************************************

        /*
         * Top Frame Settings
         */
        TitledBorder topFrameTitle;
        Border blackLine = BorderFactory.createLineBorder(Color.black);
        Border raisedBevel = BorderFactory.createRaisedBevelBorder();
        Border loweredBevel = BorderFactory.createLoweredBevelBorder();
        Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel);
        topFrameTitle = BorderFactory.createTitledBorder(compound, "Character");
        topFrameTitle.setTitleJustification(TitledBorder.CENTER);
        topFrame.setBorder(topFrameTitle);
        topFrame.setLayout(new BoxLayout(topFrame, BoxLayout.X_AXIS));


        /*
         * Adds Character Picture
         */
        charPic = new JLabel("", null, JLabel.CENTER);
        charPic.setPreferredSize(new Dimension(100,100));
        topFrame.add(charPic);

        //*******************************************************************************

        /*
        * Selection Pane Settings
        */
        selPane.setLayout(new GridBagLayout());
        selPane.setBorder(blackLine);

        /*
        * Adds Character Selection ComboBox
        */
        charSelCombo.setPrototypeDisplayValue(charSelCombo.getItemAt(unselectedDefaultElement));
        selPane.add(charSelCombo, setGbc(0,0, "WEST", 0, 1, setInsets(0, 10, 0, 0)));

        /*
        * Adds "Pick Current Level:" Label
        */
        selPane.add(pickLvl, setGbc(0,1, "EAST", 0, 1, setInsets(0, 0, 0, 0)));

        /*
        * Adds "Next Level:" Label
        */
        selPane.add(nextLvl, setGbc(0,2, "EAST", 0, 1, setInsets(0, 0, 0, 0)));

        /*
        * Adds Character Current Level ComboBox
        */
        pickLvlAns.setPrototypeDisplayValue(pickLvlAns.getItemAt(lvlRange.length - 1));
        selPane.add(pickLvlAns, setGbc(1,1, "WEST", 1, 1, setInsets(0, 10, 0, 0)));

        /*
        * Adds Character Next Level Label
        */
        selPane.add(nextLvlAns, setGbc(1,2, "WEST", 1, 1, setInsets(0, 23, 0, 0)));

        //*******************************************************************************

        /*
         * BOTTOM PANE
         */
        TitledBorder bottomFrameTitle;
        bottomFrameTitle = BorderFactory.createTitledBorder(compound, "Stats");
        bottomFrameTitle.setTitleJustification(TitledBorder.CENTER);
        bottomFrame.setBorder(bottomFrameTitle);

        //*******************************************************************************

        /*
         * Display everything in GUI to user
         */
        add(topFrame, BorderLayout.NORTH);
        add(bottomFrame,BorderLayout.CENTER);

        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent arg0) {
        String charName = ((JComboBox)(arg0.getSource())).getSelectedItem().toString();
        String image = "../images/"+charName+".png";
        charPic.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource(image)).getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH)));
        charSelCombo.removeItem(GuiCharSel[unselectedDefaultElement]);
        pickLvlAns.removeItem(lvlRange[unselectedDefaultElement]);
    }

    private GridBagConstraints setGbc(int gridx, int gridy, String anchorLocation, double weightx, double weighty, Insets insets){
        GridBagConstraints gbc = new GridBagConstraints();

        if (anchorLocation.toUpperCase().equals("NORTHWEST")){
            gbc.anchor = GridBagConstraints.NORTHWEST;
        } else if (anchorLocation.toUpperCase().equals("NORTH")){
            gbc.anchor = GridBagConstraints.NORTH;
        } else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
            gbc.anchor = GridBagConstraints.NORTHEAST;
        } else if (anchorLocation.toUpperCase().equals("WEST")){
            gbc.anchor = GridBagConstraints.WEST;
        } else if (anchorLocation.toUpperCase().equals("EAST")){
            gbc.anchor = GridBagConstraints.EAST;
        } else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
            gbc.anchor = GridBagConstraints.SOUTHWEST;
        } else if (anchorLocation.toUpperCase().equals("SOUTH")){
            gbc.anchor = GridBagConstraints.SOUTH;
        } else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
            gbc.anchor = GridBagConstraints.SOUTHEAST;
        } else {
            gbc.anchor = GridBagConstraints.CENTER;
        }

        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = insets;

        return gbc;
    }

    private Insets setInsets(int top, int left, int bottom, int right){
        Insets insets = new Insets(top,left,bottom,right);
        return insets;
    }

    public static void main(String[] args) {
        new StartGui();
    }
}

如果有人能告诉我是否有办法做到这一点,那真的会有很大帮助

编辑: 在 MadProgrammer 的帮助下,我对上面的代码进行了一些调整,使其真正起作用。

weightxweighty 确定剩余 space 中有多少提供给给定的 column/row。如果您为两个或多个 columns/rows 提供 weightx/y 值,space 将在它们之间分配,因此,设置第 1 列和第 2 列的 weightx,就像您所做的那样,意味着它们共享 space 左侧的 50%,这就是它看起来像现在这样的原因。

相反,只有最后一列真正需要 weightx

selPane.setLayout(new GridBagLayout());
selPane.setBorder(blackLine);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 2, 2);
charSelCombo.setPrototypeDisplayValue("Hello");
selPane.add(charSelCombo, gbc);
gbc.gridy++;
selPane.add(pickLvl, gbc);
gbc.gridy++;
selPane.add(nextLvl, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 1;
selPane.add(pickLvlAns, gbc);
gbc.gridy++;
selPane.add(nextLvlAns, gbc);

我稍微简化了代码(对我来说)。 GridBagConstraints 的一个好处是,GridBagLayout 会复制你提供的约束,所以你可以在多个组件之间共享它,就像我上面的那样,它使更新和修改变得更容易,因为你保持组件之间的整体关系