GridBagLayout将按钮水平堆叠而不是垂直排列,尽管gridy值递增。

huangapple go评论75阅读模式
英文:

GridBagLayout Stacking Buttons Horizontally rather than vertically, despite ascending gridy value

问题

I'm trying to get these three buttons to stack vertically, and I'm using gridy to ascend the value each time, however the buttons are appearing side by side, as shown below:

我尝试让这三个按钮垂直堆叠,并且我使用gridy来逐次增加值,但按钮却是并排显示的,如下所示:

Here is my StandardButton Class I'm using:

这是我使用的StandardButton类:

        
    ActionListener listener;

    public StandardButton(String text,Color backgroundColor, Color foregroundColor, Font font, LineBorder border, Integer width, Integer height, ActionListener listener) {
        super(text);
        this.actionListener = listener;
        
        setBackground(backgroundColor);
        setForeground(foregroundColor);
        setFont(font);
        setBorder(border);
        setPreferredSize(new Dimension(width,height));
        addActionListener(listener);
    }
    
   
    public StandardButton(String text,Color color, Integer height, ActionListener listener) {
        super(text);
        setBackground(MyLapsCompanionColorPreferences.retrieveMainForegroundColor());
        setForeground(MyLapsCompanionColorPreferences.retrieveAccentForegroundColor());
        setFont(MyLapsCompanionPreferences.retrieveMainFont());
        setBorder(BorderFactory.createLineBorder(color,2));
        Dimension dim = new Dimension();
        dim.height = height;
        setPreferredSize(dim);
        addActionListener(listener);
    }
    
}

I've tried just about every anchor setting, but this one seems to be working on my other classes in my program, so I'm not sure what the issue is here :(. Please could someone help

我尝试了几乎所有的锚定设置,但这个在我程序中的其他类上似乎有效,所以我不确定问题出在哪里 :(. 请问有人可以帮助吗?

英文:

I'm trying to get these three buttons to stack vertically, and I'm using gridy to ascend the value each time, however the buttons are appearing side by side, as shown below:

GridBagLayout将按钮水平堆叠而不是垂直排列,尽管gridy值递增。

public class AdminAreaMainPanel extends JPanel {

    StandardButton outputXMLButton, setUpRaceWeekendButton, editRaces;

    public AdminAreaMainPanel(ActionListener parentListener) {

        super(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipady = 5;
        gc.ipadx = 5;
        gc.anchor = GridBagConstraints.NORTH;
        outputXMLButton = new StandardButton("Output XML File", Color.white, Color.green, MyLapsCompanionPreferences.retrieveMainFont(),
                (LineBorder) BorderFactory.createLineBorder(MyLapsCompanionColorPreferences.retrieveAccentBackgroundColor(), 2), 200, 30, parentListener);
        add(outputXMLButton);

//        gc.anchor = GridBagConstraints.LAST_LINE_END;
        gc.gridy = 1;
        setUpRaceWeekendButton = new StandardButton("Set Up - Previous Timings", Color.white, Color.green, MyLapsCompanionPreferences.retrieveMainFont(),
                (LineBorder) BorderFactory.createLineBorder(MyLapsCompanionColorPreferences.retrieveAccentBackgroundColor(), 2), 200, 30, parentListener);
        setUpRaceWeekendButton.setToolTipText("Set up race weekend using the race timings and names from previously");
        add(setUpRaceWeekendButton);

        gc.gridy = 2;
        editRaces = new StandardButton("Edit Races", Color.white, Color.green, MyLapsCompanionPreferences.retrieveMainFont(),
                (LineBorder) BorderFactory.createLineBorder(MyLapsCompanionColorPreferences.retrieveAccentBackgroundColor(), 2), 200, 30, parentListener);
        add(editRaces);

    }

}

Here is my StandardButton Class I'm using:

        
    ActionListener listener;

    public StandardButton(String text,Color backgroundColor, Color foregroundColor, Font font, LineBorder border, Integer width, Integer height, ActionListener listener) {
        super(text);
        this.actionListener = listener;
        
        setBackground(backgroundColor);
        setForeground(foregroundColor);
        setFont(font);
        setBorder(border);
        setPreferredSize(new Dimension(width,height));
        addActionListener(listener);
    }
    
   
    public StandardButton(String text,Color color, Integer height, ActionListener listener) {
        super(text);
        setBackground(MyLapsCompanionColorPreferences.retrieveMainForegroundColor());
        setForeground(MyLapsCompanionColorPreferences.retrieveAccentForegroundColor());
        setFont(MyLapsCompanionPreferences.retrieveMainFont());
        setBorder(BorderFactory.createLineBorder(color,2));
        Dimension dim = new Dimension();
        dim.height = height;
        setPreferredSize(dim);
        addActionListener(listener);
    }
    
}

I've tried just about every anchor setting, but this one seems to be working on my other classes in my program, so I'm not sure what the issue is here :(. Please could someone help

答案1

得分: 0

你设置了属性 gc,但实际上并没有在任何地方使用 gc

将这些代码更改为:

add(outputXMLButton, gc);
add(setUpRaceWeekendButton, gc);
add(editRaces, gc);
英文:

You’re setting the properties gc but you’re not actually using gc anywhere.

Change these:

add(outputXMLButton);
add(setUpRaceWeekendButton);
add(editRaces);

to this:

add(outputXMLButton, gc);
add(setUpRaceWeekendButton, gc);
add(editRaces, gc);

huangapple
  • 本文由 发表于 2020年8月12日 05:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63366493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定