繁体   English   中英

调整 JFrame 的大小时,滚动条消失

[英]ScrollBar vanishes when JFrame is resized

我创建了一个带有JTableJScrollPane 当表格的高度大于滚动窗格的高度时,会出现一个滚动条。 如果最小化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 size 向布局管理器建议元素应该具有的尺寸

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

    公共 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);

//设置我们的布局管理器 mainFrame.setLayout(new 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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM