为啥 JOptionPane 中的 JScrollPane 不显示其所有内容?

Posted

技术标签:

【中文标题】为啥 JOptionPane 中的 JScrollPane 不显示其所有内容?【英文标题】:Why JScrollPane in JOptionPane not showing all its content?为什么 JOptionPane 中的 JScrollPane 不显示其所有内容? 【发布时间】:2013-01-29 05:17:13 【问题描述】:

我正在尝试将组件添加到 JPanel,然后将此面板放入 JScrollPane,然后将 JScrollPane 放入 JOptionPane。

问题:只添加了 19 行组件。有一个用于确定组件行数的 for 循环,如果将条件计数器更改为 19 或更少,则会显示所有组件。

这是一个SSCCE问题

import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class DynamicGroupLayout extends JPanel


    JButton addRecordButton;
    JTextField[] FieldsArray;
    JLabel[] LabelsArray;
    GroupLayout layout;
    JScrollPane scrollPane;

    public DynamicGroupLayout()
    
        addRecordButton = new JButton("add record");
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        scrollPane = new JScrollPane(this);
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        scrollPane.setPreferredSize(this.getPreferredSize());

        setTextFields();
        showDialog();
    

    @Override
    public Dimension getPreferredSize()
    
        return new Dimension(400, 500);
    

    private void setTextFields()
    
        int num = 30;  //If 19 or less, all components shown.
        String[] labelsNames =
        
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
            "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"
        ;
        FieldsArray = new JTextField[num];
        LabelsArray = new JLabel[num];

        GroupLayout.ParallelGroup parallelGroupHoriz = layout.createParallelGroup();
        layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(parallelGroupHoriz));
        for (int i = 0; i < num; i++)
        
            System.out.println(i);
            LabelsArray[i] = new JLabel(labelsNames[i]);
            FieldsArray[i] = new JTextField(10);
            FieldsArray[i].setMaximumSize(new Dimension(200, 3));
            LabelsArray[i].setLabelFor(FieldsArray[i]);
            parallelGroupHoriz.addGroup(layout.createSequentialGroup()
                    .addComponent(LabelsArray[i])
                    .addComponent(FieldsArray[i]));
        
        parallelGroupHoriz.addComponent(addRecordButton, GroupLayout.Alignment.CENTER);

        GroupLayout.SequentialGroup sequentialGroupVert = layout.createSequentialGroup();

        layout.setVerticalGroup(sequentialGroupVert);
        for (int i = 0; i < num; i++)
        
            sequentialGroupVert.addGroup(layout.createParallelGroup().
                    addComponent(LabelsArray[i])
                    .addComponent(FieldsArray[i]));
        
        sequentialGroupVert.addComponent(addRecordButton);

        for (int i = 0; i < num; i++)
        
            layout.linkSize(SwingConstants.HORIZONTAL, LabelsArray[i], LabelsArray[0]);
        
    

    private void showDialog()
    
        JOptionPane.showOptionDialog(null, scrollPane, "Update data",
                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
                null, null);
    

    public static void main(String[] args)
    
        DynamicGroupLayout d = new DynamicGroupLayout();
    

【问题讨论】:

+1 代表sscce;另请参阅Q&A。 @trashgod 谢谢垃圾神,但我的问题不是“如何使用 GroupLayout 迭代地添加组件”。我已经添加了它们,但并不是所有的都显示出来了。 添加了19个以上但在Viewport中不可见,换成return new Dimension( 400, 900 )可以看看 @oliholz 谢谢,那么如何在不改变尺寸的情况下滚动它们。我的意思是 JScrollPane 滚动条。 【参考方案1】:

如相关example 所示,您可以覆盖视口的首选大小。

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * @see https://***.com/a/14858272/230513
 * @see https://***.com/a/8504753/230513
 * @see https://***.com/a/14011536/230513
 */
public class DynamicGroupLayout 

    private static final int NUM = 30;
    private JTextField[] fields = new JTextField[NUM];
    private JLabel[] labels = new JLabel[NUM];

    private JPanel create() 
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.ParallelGroup parallel = layout.createParallelGroup();
        layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(parallel));
        GroupLayout.SequentialGroup sequential = layout.createSequentialGroup();
        layout.setVerticalGroup(sequential);
        for (int i = 0; i < NUM; i++) 
            labels[i] = new JLabel(String.valueOf(i + 1), JLabel.RIGHT);
            fields[i] = new JTextField(String.valueOf("String " + (i + 1)));
            labels[i].setLabelFor(fields[i]);
            parallel.addGroup(layout.createSequentialGroup().
                addComponent(labels[i]).addComponent(fields[i]));
            sequential.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
                addComponent(labels[i]).addComponent(fields[i]));
            layout.linkSize(SwingConstants.HORIZONTAL, labels[i], labels[0]);
        
        return panel;
    

    public static void main(String[] args) 
        EventQueue.invokeLater(new Runnable() 
            @Override
            public void run() 
                final JPanel panel = new DynamicGroupLayout().create();
                JScrollPane jsp = new JScrollPane(panel) 
                    @Override
                    public Dimension getPreferredSize() 
                        return new Dimension(320, 240);
                    
                ;
                JOptionPane.showMessageDialog(null,
                    jsp, "Data", JOptionPane.PLAIN_MESSAGE);
            
        );
    

【讨论】:

以上是关于为啥 JOptionPane 中的 JScrollPane 不显示其所有内容?的主要内容,如果未能解决你的问题,请参考以下文章

java中,swing设计中,为啥JOptionPane.showmessageDialog()弹出对话框后,单击确定按钮能关闭窗口?

php 使用Laravel和jScroll进行无限滚动分页

JOptionPane 中的文本换行?

如何处理 JOptionPane 中的取消按钮

为啥我不能输入我的号码?

如何更改 JOptionPane.showInputDialog 中按钮的默认文本