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

时间:2017-04-14 07:40:37

标签: java swing layout-manager gridbaglayout

我是Java Swing的新手,我正在做一个小项目来帮助我熟悉它。我试图指定我的对象的放置尺寸(以及它们的尺寸),但我似乎无法做到正确。我的对象大小呈现正确,但不能将它们放在我想要的位置。以下是目前的样子,以及我正在寻找的内容......

目前...

enter image description here

需要让它看起来像......

enter image description here

以下是我打鼓的代码......

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的帮助下,我对上面的代码进行了一些调整,实际上它已经成功了。

1 个答案:

答案 0 :(得分:2)

weightxweighty确定向给定列/行提供了多少剩余空间。如果您将权重x / y值提供给两个或更多列/行,则空间在它们之间划分,因此,设置第1列和第2列的weightx,就像您拥有的那样,意味着它们共享50%的空间的左边,这就是它看起来如此的原因。

相反,只有最后一列确实需要weightx

Example

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将复制您提供的约束,因此您可以在多个组件之间共享它,就像我上面一样,它使更新和修改更容易,因为你保持组件之间的整体关系