GridBagLayout组件未按预期填充空间 [英] GridBagLayout component not filling space as it should

查看:53
本文介绍了GridBagLayout组件未按预期填充空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用许多不同组件的程序,因此我开始为其使用GridBagLayout.效果很好,但是当我运行程序时,我得到了这个信息:

I'm making a program that uses lots of different compenents, so I've started using a GridBagLayout for it. It's working great, but when I run the program I get this:

当前布局的图片

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

It should be putting the right-most list object directly up against the rest of the objects, and filling the entire width of the screen. Not sure why it isn't. Here's a short copy of the relevant code (I put them all into one class for simplicity should you decide to run it.):

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类,因为它是发生故障的那一类,我认为这是最相关的.

I included all of the NoteBox class because it's the one that's malfunctioning and I figured it was the most relevant.

推荐答案

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

I simply had to put weightx = 1 within my NoteBox inner class, as suggested by MadProgrammer. This fixed the issue entirely, I'm just writing this to formally mark the answer as solved.

这篇关于GridBagLayout组件未按预期填充空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆