小编典典

GridBagLayout组件未按预期填充空间

java

我正在编写一个使用许多不同组件的程序,因此我开始对其使用GridBagLayout。效果很好,但是当我运行程序时,我得到了:

当前布局图

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

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类,因为它是发生故障的类,并且我认为它是最相关的。


阅读 193

收藏
2020-11-26

共1个答案

小编典典

正如MadProgrammer建议的那样,我只需要在我的NoteBox内部类中放入weightx =
1即可。这样就完全解决了这个问题,我只是在写这篇文章,正式将答案标记为已解决。

2020-11-26