1

我看过关于这个主题的其他帖子,但他们找到的解决方案不适用于我。我正在设置一个加权值并使用c.fill = GridBagConstraints.BOTH约束。

我包含了我拥有的整个 GUI 代码,以防万一我的错误来自 GridBagLayout 以外的其他东西。

我希望右侧的可滚动文本块扩展 GUI 中的剩余空间,并且我已经设置了所有应该归因于它的变量,但它仍然无法正常工作。我究竟做错了什么?

我的结果:

GridBagLayout 结果

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

public class TestCode extends JFrame {
    JTextArea textArea = new JTextArea ();
    JComboBox <String> typeComboBox;
    JTextField searchField;
    JTextField fileField;

    public TestCode(){

        setTitle ("GUI Test");
        setSize (600, 300);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible (true);

        JScrollPane scrollPane = new JScrollPane(textArea);

        JButton readButton = new JButton("Read File");
        JButton displayButton = new JButton("Display");
        JButton searchButton = new JButton("Search");


        searchField = new JTextField(10);
        fileField = new JTextField(15);

        typeComboBox = new JComboBox <String> ();
        typeComboBox.addItem("Index");
        typeComboBox.addItem("Type");
        typeComboBox.addItem("Name");




        JPanel container = new JPanel();
            container.setLayout(new GridBagLayout());
            container.setPreferredSize(new Dimension(250, 100));
        JPanel filePanel = new JPanel();
            filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
            filePanel.add(new JLabel("Source file", SwingConstants.LEFT));
                JPanel filePanelTop = new JPanel();
                    filePanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
                    filePanelTop.add(fileField);
                JPanel filePanelBottom = new JPanel();
                    filePanelBottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
                    filePanelBottom.add(readButton);
                    filePanelBottom.add(displayButton);
            filePanel.add(filePanelTop);
            filePanel.add(filePanelBottom);
            filePanel.setMaximumSize(filePanel.getPreferredSize());
            filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
        JPanel searchPanel = new JPanel(); 
            searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
            searchPanel.add(new JLabel("Search target", SwingConstants.LEFT));
                JPanel searchPanelTop = new JPanel();
                    searchPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
                    searchPanelTop.add(searchField);
                    searchPanelTop.add(typeComboBox);
            searchPanel.add(searchPanelTop);
            searchPanel.add(searchButton);
            searchPanel.setMaximumSize(searchPanel.getPreferredSize());
            searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));

          GridBagConstraints c = new GridBagConstraints();
          c.gridx = 0;
          c.gridy = 0;
          container.add(filePanel, c);
          c.gridx = 0;
          c.gridy = 1;
          container.add(searchPanel, c);
          c.gridx = 1;
          c.gridy = 0;
          c.weightx = 1.0;
          c.weighty = 1.0;
          c.gridwidth = GridBagConstraints.REMAINDER;
          c.gridheight = GridBagConstraints.REMAINDER;
          c.fill = GridBagConstraints.BOTH;
          c.anchor = GridBagConstraints.NORTHWEST;
          container.add(scrollPane, c);

        add(container, BorderLayout.WEST);

        validate();

    } // end method toString

    public static void main(String[] args){
        TestCode run = new TestCode();
    }
} // end class Treasure
4

1 回答 1

7
//add(container, BorderLayout.WEST);
add(container);

West 将组件限制在其首选宽度。默认值为 CENTER,它允许组件扩展以填充可用空间。

另外,您代码的主要结构是错误的。您应该首先将所有组件添加到框架中,然后调用:

frame.pack();
frame.setVisible(true);

那么就不需要 validate()。

于 2013-08-29T23:05:21.203 回答