gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:38:20 24 4
gpt4 key购买 nike

我有一个带 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com