调整 JFrame 大小时,ScrollBar 消失

ScrollBar vanishes when JFrame is resized

我创建了一个 JScrollPane,上面有一个 JTable。当table的高度大于滚动条的高度时,出现滚动条。如果最小化 JFrame 虽然我没有改变它的大小,滚动条消失并且滚动窗格向下延伸。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Main {
    
    static String columns[] = {"Date", "Price", "URL", "Expired"};
    static String data[][] = new String[8][4];
    
    /*
     * The data that should be provided to the JTable is 
     * replaced with some example data because the method
     * of getting this data is complicated and doesn't
     * change anything at the outcome.
     */

    public static void main(String[] args) {
        
        loadGui();
    }
    
    public static void loadGui() {
        
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 4; j++) {
                data[i][j] = "Example data " + i + " " + j;
            }
        }
        
        JFrame mainFrame = new JFrame();
        mainFrame.setSize(800, 300);
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
        
        JTable table = new JTable(data, columns);;
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        
        JScrollPane pane = new JScrollPane(table);
        pane.setViewportView(table);
        pane.setSize(785, 100);
        mainFrame.add(pane);
        table.getTableHeader().setReorderingAllowed(false);
        table.setDefaultEditor(Object.class, null);
        table.setFocusable(false);
        table.setRowSelectionAllowed(false);
    }
}

我正在寻找一种方法来阻止滚动窗格向下扩展并保持其大小。

  1. 您可以使用 BorderLayout 布局管理器使您的滚动窗格固定在顶部

  2. 如果可能,您可以使用 setPreffered 大小向布局管理器建议元素应具有的尺寸

  3. 当所有组件都已添加时使用setVisible

    public class 主 {

    static String columns[] = {"Date", "Price", "URL", "Expired"};
    static String data[][] = new String[8][4];
    
    /*
     * The data that should be provided to the JTable is 
     * replaced with some example data because the method
     * of getting this data is complicated and doesn't
     * change anything at the outcome.
     */
    
    public static void main(String[] args) {
    
        loadGui();
    }
    
    public static void loadGui() {
    
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 4; j++) {
                data[i][j] = "Example data " + i + " " + j;
            }
        }
    
        JFrame mainFrame = new JFrame();
        mainFrame.setSize(800, 300);
        mainFrame.setResizable(false);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

//设置我们的layer manager mainFrame.setLayout(新的 BorderLayout());

        JTable table = new JTable(data, columns);;
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.getTableHeader().setReorderingAllowed(false);
        table.setDefaultEditor(Object.class, null);
        table.setFocusable(false);
        table.setRowSelectionAllowed(false);


        JScrollPane pane = new JScrollPane(table);
        pane.setViewportView(table);
// we would like to have its height at 100px, width does not matter thus 0
        pane.setPreferredSize(new Dimension(0,100));

//using NORTH will "stick" the component to the top
           mainFrame.add(pane,BorderLayout.NORTH);

//all the calculation will be done now. The same happens when you minimize/restore the frame.
            mainFrame.setVisible(true);
    }
}