当我使用 GridBagLayout 为 JPanel 中的每个项目设置边框颜色时,组件会崩溃。
但是当我只删除一个组件的边框时,填充保持不变。
我究竟做错了什么?
边框设置:
setBorder(BorderFactory.createLineBorder(Color.decode("#"+Constants.Display.OPTIONS_BORDER_COLOR)));
面板:
public class OptionsPanel extends JPanel {
private AddMachineBtn addMachineBtn;
private SearchField searchField;
private SearchBtn searchBtn;
private GridBagConstraints gbc;
public OptionsPanel() {
init();
config();
build();
}
private void init() {
addMachineBtn = new AddMachineBtn("Add Machine");
searchField = new SearchField("Search...");
searchBtn = new SearchBtn("S");
gbc = new GridBagConstraints();
int i = Constants.Display.OPTIONS_PANEL_PADDING;
gbc.insets = new Insets(i, i, i, i);
}
private void config() {
setLayout(new GridBagLayout());
setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.decode("#"+Constants.Display.OPTIONS_BORDER_COLOR)));
setPreferredSize(new Dimension(0, Constants.Display.OPTIONS_PANEL_HEIGHT));
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = gbc.LINE_START;
}
private void build() {
gbc.gridx = 0;
add(addMachineBtn, gbc);
gbc.weightx = 0;
gbc.gridx = 1;
add(searchField, gbc);
gbc.gridx = 2;
add(searchBtn, gbc);
}
}