我想创建一个看起来像附图的布局。它有2个面板。最小宽度的左侧面板为500px,调整JFrame大小时调整大小。右侧面板的固定宽度为120px。并且它们之间有10px的填充。
我尝试了GridBagLayout,但似乎没有按预期工作。请帮忙。谢谢。
JPanel leftBox;
JPanel rightBox;
JButton btnSave;
JButton btnRefresh;
JTextArea txtArea;
leftBox = new JPanel();
rightBox = new JPanel();
btnSave = new JButton("Save");
btnRefresh = new JButton("Refresh");
txtArea = new JTextArea();
txtArea.setFont(new Font("Consolas", Font.BOLD, 14));
leftBox.setLayout(new BoxLayout(leftBox, BoxLayout.Y_AXIS));
leftBox.add(txtArea);
rightBox.setLayout(new BoxLayout(rightBox, BoxLayout.Y_AXIS));
rightBox.add(btnSave);
rightBox.add(btnRefresh);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
this.add(leftBox, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
this.add(rightBox, gbc);
txtArea.append("-------------");