java - 无法设置 JTextPane/JScrollPane 的大小

标签 java swing jscrollpane layout-manager jtextpane

我有一个带 BorderLayout 的 JFrame,在北边有一个 JTextPane/JScrollPane,在中间有一个分割 Pane ,在南边有一个带有 4 个 JButtons 的 JPanel。我的框架尺寸是 (500, 470)。我希望 JTextPane 填充大部分窗口,因此我将其首选大小设置为 (500, 450),但没有效果。

这是一张关于所发生情况的图片:http://postimg.org/image/3wslmppx7/

这是我的代码:

public void initGUI()
{
    configWindow = new JFrame();
    configWindow.setTitle(node.getDispName()+ " [" + node.getIp() + "] configuration");
    configWindow.setSize(500, 470);
    configWindow.setLocation(TopologyMain.dim.width/2-configWindow.getSize().width/2, TopologyMain.dim.height/2-configWindow.getSize().height/2);
    configWindow.getContentPane().setLayout(new BorderLayout());

    configPane = new JTextPane();
    configPane.setEditable(true);
    configPane.setName("Config");
    configPane.setPreferredSize(new Dimension(500, 450));
    configPane.setBackground(Color.black);
    configPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));

    scrollPane = new JScrollPane(configPane);
    scrollPane.setPreferredSize(new Dimension(500, 450));

    configWindow.getContentPane().add(scrollPane, BorderLayout.NORTH); 

    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridBagLayout());
    //buttonPanel.setPreferredSize(new Dimension(500, 20));
    GBC.insets = new Insets(5,5,5,5);

    read = new JButton("Read");
    GBC.gridx = 0;
    GBC.gridy = 1;
    GBC.fill = GridBagConstraints.HORIZONTAL;
    buttonPanel.add(read, GBC);

    apply = new JButton("Apply");
    GBC.gridx = 1;
    GBC.gridy = 1;
    GBC.fill = GridBagConstraints.HORIZONTAL;
    buttonPanel.add(apply, GBC);

    load = new JButton("Load");
    GBC.gridx = 2;
    GBC.gridy = 1;
    GBC.fill = GridBagConstraints.HORIZONTAL;
    buttonPanel.add(load, GBC);

    save = new JButton("Save");
    save.setEnabled(false);
    GBC.gridx = 3;
    GBC.gridy = 1;
    GBC.fill = GridBagConstraints.HORIZONTAL;
    buttonPanel.add(save, GBC);

    configWindow.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, buttonPanel);
    splitPane.setEnabled(true);
    configWindow.getContentPane().add(splitPane, BorderLayout.CENTER);

    configWindow.setVisible(true);

    fileChooser = new JFileChooser();
}

最佳答案

来自Gabriel Negut的解决方案在评论中:

使用pack() - 但仅在将所有内容添加到窗口后才调用它。

来自the documentation :

public void pack()

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.

关于java - 无法设置 JTextPane/JScrollPane 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364758/

相关文章:

java - Java中的泛型,返回泛型类Set<T>的集合

java - Swing 中的切换面板

java - 将滚动条添加到我的文本区域,但我的图像未显示

java - AndroidBlockGuardPolicy.onReadFromDisk FileInputStream 违规

java - 如何比较两个包含具有共享属性的对象的数组列表

java - 线程没有从 SwingUtilities.invokeLater 返回到主线程

java - 如何使复选框执行连续任务,直到取消选中为止?

java - JScrollPane 用于包含一组带有 BoxLayout 的标签的面板

javascript - 具有动态加载内容的 jScrollpane,不滚动

java - 在 Spring Boot Hibernate 搜索中建立索引时屏蔽敏感数据