GridBagLayout 水平堆叠按钮而不是垂直堆叠按钮,尽管增加了网格值
GridBagLayout Stacking Buttons Horizontally rather than vertically, despite ascending gridy value
我试图让这三个按钮垂直堆叠,每次我都使用 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);
}
}
这是我的 StandardButton Class 我正在使用:
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);
}
}
我已经尝试了几乎所有的锚点设置,但是这个似乎在我程序中的另一个 类 上起作用,所以我不确定这里的问题是什么:(。请问有人帮忙
您正在设置属性 gc
,但实际上并未在任何地方使用 gc
。
更改这些:
add(outputXMLButton);
add(setUpRaceWeekendButton);
add(editRaces);
对此:
add(outputXMLButton, gc);
add(setUpRaceWeekendButton, gc);
add(editRaces, gc);
我试图让这三个按钮垂直堆叠,每次我都使用 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);
}
}
这是我的 StandardButton Class 我正在使用:
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);
}
}
我已经尝试了几乎所有的锚点设置,但是这个似乎在我程序中的另一个 类 上起作用,所以我不确定这里的问题是什么:(。请问有人帮忙
您正在设置属性 gc
,但实际上并未在任何地方使用 gc
。
更改这些:
add(outputXMLButton);
add(setUpRaceWeekendButton);
add(editRaces);
对此:
add(outputXMLButton, gc);
add(setUpRaceWeekendButton, gc);
add(editRaces, gc);