【问题标题】:Swing JScrollPane resizing to fit the viewport panelSwing JScrollPane 调整大小以适应视口面板
【发布时间】:2017-04-30 21:24:11
【问题描述】:

我尝试了很多布局管理器,但没有一个可以解决我的问题:

  • 我希望滚动窗格中的项目保持其大小(首选或最小),而不是调整大小(缩小)以适合视口面板。因为如果它是一个 JTextArea,并且如果文本区域有空白区域并且比视口大,它会缩小它,因此不会显示空白文本区域。我希望显示空白文本区域以解决外观问题。
  • 我正在使用 BoxLayout 一个接一个地堆叠项目,在我看来,对于文本区域,setMinimum 方法失败了。
  • 如果文本区域有空格,则 ScrollPane 的滚动条不会出现,而只会在没有空格的情况下出现。

有什么办法吗?

    JScrollPane materialPane = new FScrollPane();
    this.materialPaneView = new TPanel();
    this.materialPaneView.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
    this.materialPaneView.setLayout(new BoxLayout(this.materialPaneView, BoxLayout.Y_AXIS));
    materialPane.setViewportView(materialPaneView);
    materialPane.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
    for(Material mat: this.unit.getMaterial()){
        this.addMaterial(mat);

    }

    centerPanel.add(sectionPane);
    centerPanel.add(exercisePane);
    centerPanel.add(materialPane);
    this.add(upperPanel, BorderLayout.NORTH);
    this.add(centerPanel, BorderLayout.CENTER);

public void addMaterial(Material mat){
    JTextField matName = new JTextField(30);
    JPanel fieldButtonPanel = new TPanel();
    fieldButtonPanel.setLayout(new GridLayout(1,2));
    JPanel fieldPanel = new TPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel deleteMatButtonPanel = new TPanel(new FlowLayout(FlowLayout.RIGHT));
    matName.setText(mat.getName());
    matName.setMaximumSize(new Dimension(FFont.def.getSize()*20, 30));
    fieldPanel.add(matName);
    JButton deleteMat = new JButton("Delete Material");
    deleteMatButtonPanel.add(deleteMat);

    fieldButtonPanel.add(fieldPanel);
    fieldButtonPanel.add(deleteMatButtonPanel);
    fieldButtonPanel.setAlignmentX(LEFT_ALIGNMENT);

    JTextArea matText = new FTextArea(mat.getDesc(), (int)(WIDTH*0.95), (int)(HEIGHT/3.4));
    matText.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.5)));
    /*matText.setMaximumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.4)));*/
    matText.setText(mat.getDesc());
    matText.setAlignmentX(LEFT_ALIGNMENT);
    this.materialPaneView.add(fieldButtonPanel);

    this.materialPaneView.add(matText);
    matName.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            mat.setName(matName.getText());

        }
    });

HEIGHT 和 WIDTH 是常量,TPanel FScrollPane 是我预定义的透明面板。 BoxLayout 面板是 scrollPane 的视口,它仍然会调整文本区域的大小。

【问题讨论】:

  • 请发布您的minimal reproducible example 代码,并可能显示您观察到的行为与期望行为的图像。您当然也知道,您可以通过调用采用两个整数的构造函数来设置 JTextArea 所需的可见列和行数,对吧?
  • 是的,我确实设置了。立即发布我的代码
  • 请重新阅读minimal reproducible example链接。

标签: java swing jscrollpane


【解决方案1】:

我不确定我是否得到你的要求,所以如果我完全没有抓住重点,请告诉我......

据我所知,视口大小由 JScrollPane 内的组件控制,无论视口发生什么,JScrollPane 的大小都不会改变。

你要么想要:

A) 将 JScrollPane 调整为与其内容相同的大小。

我会实现侦听器来查找内容大小的变化并相应地调整 ScrollPane 的大小,但您也需要注意调整整个 Hierarchy 的大小。

B) 您想调整视口的大小以使其适合 JScrollPane?你知道没有滚动条。

我遇到了这个问题,并通过使用 ScrollablePanel 组件修复了它。检查此answer,点击链接下载 .class 并使用它来使用调整大小以适应 ScrollPane 的 JPanel。

那些不是非常详细的答案,但在扩展之前我需要更多关于你正在尝试做什么的信息。而且您的代码不完整,请始终共享一个我们可以CTRL + C / V并轻松验证最终问题的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2020-06-07
    相关资源
    最近更新 更多