GridBagLayout 组件未按应有的方式填充 space

GridBagLayout component not filling space as it should

我正在制作一个使用很多不同组件的程序,所以我开始为它使用 GridBagLayout。它工作得很好,但是当我 运行 我得到这个程序时:

Picture of current layout

应该是将最右边的列表对象直接放在其余对象上,并填满屏幕的整个宽度。不知道为什么不是。这是相关代码的简短副本(如果您决定 运行,为了简单起见,我将它们全部放在一个 class 中。):

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Example extends JFrame{
    private Character me;
    public static void main(String[] args){
        new Example();
    }

    public Example(){
        add(new CharacterDisplay(new Character()));
        setSize(600,500);
        setVisible(true);
    }

    private class Character{
        public ArrayList<String> notes = new ArrayList<String>();
        public Character(){
            notes.add("A");
            notes.add("few");
            notes.add("notes");
        }
    }

    private class CharacterDisplay extends JPanel{
        public CharacterDisplay(Character myself){
            me = myself;
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            JTextArea filler = new JTextArea();
            c.gridx  = 0;
            c.weightx = 1;
            c.weighty = 1;
            c.gridheight = 11;
            c.gridwidth = 6;
            c.fill = GridBagConstraints.BOTH;
            add(filler,c);

            c.gridy = 0;
            c.gridx = 6;
            c.weightx = 1;
            c.weighty = 1;
            c.gridheight = 11;
            c.gridwidth = 3;
            c.fill = GridBagConstraints.BOTH;
            add(new NoteBox(), c);
        }
    }

    private class NoteBox extends JPanel{
        private int currentNotes = 0;
        private JList<String> listContainer;
        private JTextField addNoteField = new JTextField();
        private JButton removalButton = new JButton("Remove Selected Notes");
        private JScrollPane listScroll;
        public NoteBox(){
            setLayout(new GridBagLayout());
            addNoteField.setText("Add a note here");
            addNoteField.addActionListener(e -> {
                me.notes.add(addNoteField.getText()); 
                repaint();});
            String[] model = new String[me.notes.size()];
            model = me.notes.toArray(model);
            listContainer = new JList<String>(model);
            removalButton.addActionListener(e -> remove(listContainer.getSelectedIndices()));
            listScroll = new JScrollPane(listContainer);
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.BOTH;
            c.gridwidth = 3;
            c.weighty = 0;
            add(addNoteField, c);
            c.weighty = 1;
            c.gridy = 1;
            c.gridheight = 9;
            add(listScroll, c);
            c.weighty = 0;
            c.gridy = 10;
            add(removalButton, c);
        }

        public void remove(int[] indices){
            //Go backward to avoid shifting indices of the next ones to remove;
            for(int i = indices.length - 1; i >= 0; i--){
                me.notes.remove(indices[i]);
            }
            repaint();
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            String[] model = new String[me.notes.size()];
            model = me.notes.toArray(model);
            listContainer.setListData(model);
        }
    }
}

我包括了所有的 NoteBox class,因为它是发生故障的那个,我认为它是最相关的。

我只需要按照 MadProgrammer 的建议将 weightx = 1 放在我的 NoteBox 内部 class 中。这完全解决了问题,我写这篇文章只是为了正式将答案标记为已解决。