GridBagLayout组件没有填充空间

时间:2016-05-17 21:06:24

标签: java swing gridbaglayout

我正在制作一个使用大量不同功能的程序,所以我开始使用GridBagLayout。它工作得很好,但是当我运行程序时,我得到了这个:

Picture of current layout

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

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类,因为它是一个出现故障的类,我认为它是最相关的。

1 个答案:

答案 0 :(得分:0)

我必须按照MadProgrammer的建议将weightx = 1放在我的NoteBox内部类中。这完全解决了这个问题,我只是写这个来正式将答案标记为已解决。