java - JScroll 在右侧面板 JSplitPane 中不起作用 - Swing Java

标签 java swing

你能帮我解决这个问题吗?我为 BitTorrent 的客户端编写了布局,但我不知道为什么 JScroll 在右侧面板 JSplitPane 中不起作用。更多代码在这里:https://github.com/niesuch/bittorrentclient/blob/nie_bittorrentclient/src/Bittorrent.java 提前致谢。

    /**
     * Initialization information panel
     */
    private void _initInfoPanel() {
        JPanel formPanel = new JPanel();
        formPanel.setBorder(BorderFactory.createTitledBorder("Informations"));

        JPanel form = new JPanel(new GridLayout(0, 2));

        int i = 0;
        for (String formLabel : _formLabels) {
            form.add(new JLabel(formLabel));
            _textFields[i] = new JTextField(10);
            form.add(_textFields[i++]);
        }

        formPanel.add(form);        
        _infoPanel.add(formPanel);        
        _infoPanel.add(new JScrollPane(formPanel), BorderLayout.CENTER);

    }

我的最小示例程序:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

public class Test extends JFrame
{
    private final JPanel _downloadsPanel;
    private final JPanel _buttonsPanel;
    private final JPanel _infoPanel;
    private final JTextField[] _textFields;
    private final String[] _formLabels =
    {
        "Name: ", "Size: ", "% downloaded: ", "Status: ",
        "Download: ", "Upload: ", "Time remaining: ", "Pieces: ",
        "Peer data including IP addresses: ", "Speed download from them: ",
        "Speed upload to them: ", "Port using: ", "Port client: "
    };
    private JButton _pauseButton;

    public Test() {
        setTitle("BitTorrent");
        setSize(1024, 768);

        _downloadsPanel = new JPanel();
        _buttonsPanel = new JPanel();
        _infoPanel = new JPanel();
        _textFields = new JTextField[_formLabels.length];

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, _downloadsPanel, _infoPanel);
        splitPane.setResizeWeight(0.7);

        _initInfoPanel();
        _initButtonsPanel();

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(_buttonsPanel, BorderLayout.SOUTH);
        getContentPane().add(splitPane, BorderLayout.CENTER);        
    }

    private void _initInfoPanel()
    {
        JPanel formPanel = new JPanel();
        formPanel.setBorder(BorderFactory.createTitledBorder("Informations"));

        JPanel form = new JPanel(new GridLayout(0, 2));

        int i = 0;
        for (String formLabel : _formLabels)
        {
            form.add(new JLabel(formLabel));
            _textFields[i] = new JTextField(10);
            form.add(_textFields[i++]);
        }

        formPanel.add(form);        
        _infoPanel.add(formPanel);        
        _infoPanel.add(new JScrollPane(formPanel), BorderLayout.CENTER);

    }


    private void _initButtonsPanel()
    {
        _pauseButton = new JButton("Pause");
        _pauseButton.setEnabled(false);
        _buttonsPanel.add(_pauseButton);

    }

    public static void main(String[] args)
    {
        Test bittorrent = new Test();
        bittorrent.setVisible(true);
    }

}

最佳答案

这是一个简单的错误。您将 _infoPanel 视为使用 BorderLayout,例如,

_infoPanel.add(new JScrollPane(formPanel), BorderLayout.CENTER); //!!

但它没有,而是使用 JPanel 的默认 FlowLayout:

_infoPanel = new JPanel(); 

FlowLayout 将以其首选大小显示组件,并且不会尝试缩小或展开它们,因此 JScrollPane 不会更改其大小。所以做出明显的改变:

_infoPanel = new JPanel(new BorderLayout()); 

关于java - JScroll 在右侧面板 JSplitPane 中不起作用 - Swing Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34827484/

相关文章:

java - 如何停止匿名类中的 Swing 计时器?

java - JList/ListModel getElements 不起作用

java - Joda 时间的 LocalDate 间隔

java - 如何在 MacOS 10.5.6 上运行 Eclipse 3.4.1?

java - 通用返回类型 - 为什么我不能这样做?

java - JSP 的 JTable 替代品

java - 形状消失

java - 为什么 JFrame 在添加图像时会自行调整大小

java - 如何在 Eclipse 中配置 "assign parameter to new field"quickfix 的行为?

java - Selenium webdriver 壁虎错误 : The path to the driver must be set by webdriver. 壁虎