当前位置: 首页 > 面试题库 >

GridBagLayout组件未按预期填充空间

苏宏逸
2023-03-14
问题内容

我正在编写一个使用许多不同组件的程序,因此我开始对其使用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类,因为它是发生故障的类,并且我认为它是最相关的。


问题答案:

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



 类似资料:
  • 同样,在导出时没有错误。问题是getCityWeatherbyZipresponse.getCityWeatherbyZipResult的值为null。我知道文档正在返回正确的结果,因为结果打印如下: 结果打印输出: 测试Web服务:http://wsf.cdyne.com/weatherws/weather.asmx 子umarshal对象:

  • 我正在使用spring Roo并希望访问Controller类中的一个bean,该类在ApplicationContext.xml中具有以下配置: 配置类本身是: 在我的Controller中,我认为一个简单的Autowired注释应该可以完成这项工作 在启动过程中,spring在setSkipWeeks方法中打印消息。不幸的是,每当我在控制器中调用config.getSkipWeeks()时,它

  • 当我运行以下程序时,它只打印 然而,从Java 8的equalsIgnoreCase文档中我们发现: 如果以下至少一项为真,则两个字符c1和c2被视为相同的忽略情况: •对每个字符应用java.lang.character.ToUpperCase(char)方法会产生相同的结果 所以我的问题是为什么这个程序不打印 在这两种操作中,都使用了大写字符。

  • 我试图使用来传输我根据前面的问题设置的自定义标头。 我在文件中读到... 我的属性包括:

  • 我正在和selenium一起工作,刮一些数据。 有一个按钮在页面上,我正在点击说“Custom_Cols”。这个按钮为我打开了一个窗口,我可以在那里选择我的列。 我的问题是为什么新窗口上的元素不可见,即使我正在等待元素的可见。补充一下,我已经尝试增加延迟时间,但我还是会偶尔出现这个错误。 我的密码在这里