GridBagConstraints ipad锁定大小

时间:2017-04-14 21:26:45

标签: java swing layout-manager gridbaglayout

我是Java Swing的新手,正在开展一个项目,帮助我更熟悉它。我在更改标签文本时注意到,GridBagLayou t中的单元格增加/减少(您可以通过边界线调整大小来判断)。我想知道是否有办法锁定ipad大小,所以它不会改变(在设置之后)。有没有办法锁定ipad大小?

下面是图片,您可以看到我在说什么。

在:

enter image description here

后:

enter image description here

注意当放入一个数字时标签如何缩小。如果放入一个双位数,标签会扩大(大于" - - "标签)

以下是代码:

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StartGuiTest extends JFrame implements ActionListener {
    private static final int unselectedDefaultElement = 0;
    private static final String unselectedLvl = "- -";
    private static final int maxLvl = 99;
    private static final String[] GuiCharSel = {"---  Select Character ---", "Cloud", "Barret", "Tifa", "Aeris", "Red XIII", "Yuffie", "Cait Sith", "Vincent", "Cid"};
    private String[] lvlRange = createArrRange(unselectedLvl, 1, maxLvl);

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

    public StartGuiTest() {
        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:");

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

        /*
         * Adds Character Selection ComboBox
         */
        charSelCombo.setPrototypeDisplayValue(charSelCombo.getItemAt(unselectedDefaultElement));
        selPane.add(charSelCombo, setGbc(0,0, 0, 0, "WEST", 0, 1, setInsets(0, 10, 0, 0)));
        charSelCombo.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        charSelCombo.removeItem(GuiCharSel[unselectedDefaultElement]);
                        pickLvlAns.removeItem(lvlRange[unselectedDefaultElement]);
                    }
                }
        );

        /*
         * Adds "Pick Current Level:" Label
         */
        selPane.add(pickLvl, setGbc(0,1, 0, 0, "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, 0, 0, "WEST", 1, 1, setInsets(0, 10, 0, 0)));
        pickLvlAns.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String currLvl = ((JComboBox)(e.getSource())).getSelectedItem().toString();
                        if(isInteger(currLvl)){
                            if (Integer.parseInt(currLvl) == maxLvl){
                                nextLvlAns.setText(unselectedLvl);
                            } else {
                                nextLvlAns.setText(Integer.toString(Integer.parseInt(currLvl) + 1));
                            }
                        } else {
                            nextLvlAns.setText(unselectedLvl);
                        }
                    }
                }
        );

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

        /*
         * Adds Character Next Level Label
         */
        nextLvlAns.setBorder(blackLine);
        nextLvlAns.setHorizontalAlignment(JLabel.CENTER);
        selPane.add(nextLvlAns, setGbc(1,2, 28, 5, "WEST", 1, 1, setInsets(0, 10, 0, 0)));

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

        topFrame.add(selPane);

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

        /*
         * 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) {

    }

    private GridBagConstraints setGbc(int gridx, int gridy, int ipadx, int ipady, 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.ipadx = ipadx;
        gbc.ipady = ipady;
        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;
    }

    protected static String[] createArrRange(String firstElement, int startNum, int endNum) {
        String[] strArr = new String[endNum+1];
        strArr[0] = firstElement;
        for (int num = startNum, element = 1; num <= endNum; num++, element++){
            strArr[element] = Integer.toString(num);
        }
        return strArr;
    }

    public static boolean isInteger(String s) {
        try {
            Integer.parseInt(s);
        } catch(NumberFormatException e) {
            return false;
        } catch(NullPointerException e) {
            return false;
        }
        // only got here if we didn't return false
        return true;
    }

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

我尝试过使用label.setPrototypeDisplayValue(),但我想这只适用于锁定它们的组合框,因此它们不会改变大小。我似乎无法在图书馆或谷歌中找到任何显示如何执行此操作的内容。

1 个答案:

答案 0 :(得分:3)

欢迎来到可变宽度字体的精彩世界。

我会认为问题不在于nextLvlAns,而在于pickLvlAns

问题似乎是--2的大小不同,这会改变组合框的大小。

您使用setPrototypeDisplayValue开始朝着正确的方向前进,但我建议您使用长pickLvlAns.setPrototypeDisplayValue("00");或甚至pickLvlAns.setPrototypeDisplayValue("----");这样的内容,以便它涵盖更大的内容可能的范围。

请注意,使用可变宽度字体时,0可能会更大-

另一个技巧可能是使用不可编辑的JTextField代替JLabel,这是因为JLabel一直希望自己适应文字内容

JTextField nextLvlAns = new JTextField(unselectedLvl, 3);
//...
nextLvlAns.setEditable(false);