JScrollpane占用整个Jframe

发布于 2025-01-19 13:27:56 字数 2592 浏览 4 评论 0原文

我正在尝试为任务列表创建一个表单,该表格将在jscrollpane中具有一系列任务列表,其下面有一些按钮,从本质上讲是这样的:

我遇到的问题是滚动窗格似乎正在接管整个框架,我看不到按钮。我正在制作一个更简单的原型,以试图弄清楚我在做什么错,但我似乎没有什么可用。

有人可以帮助我包含JSCrollpane的大小吗?

public class Test extends JFrame {

    private JLabel label1 = new JLabel();
    private JLabel label2 = new JLabel();
    private JLabel[] titles;
    private JLabel[] descriptions;
    private JPanel[] panels;
    private JCheckBox[] boxes;
    private JScrollPane jScrollPane1;
    private JPanel bigPanel;
    private final static int NUM_OF_RESULTS = 10;

    public Test() {

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(1000, 600));

        bigPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        //set layout to
        GridLayout layout = new GridLayout(30, 30);
        bigPanel.setLayout(layout);
        jScrollPane1 = new JScrollPane(bigPanel);
        jScrollPane1.setPreferredSize(new Dimension(500, 300));
        getContentPane().add(jScrollPane1);

        // How do I get these buttons to show on the main panel?
        // JButton button1 = new JButton("Save");
        // JButton button1 = new JButton("Cancel");
        // getContentPane.add(button1);
        // getContentPane.add(button2);

        scrollPanelDemo();
        pack();
        setVisible(true);
    }

    public void scrollPanelDemo() {

        titles = new JLabel[NUM_OF_RESULTS];
        descriptions = new JLabel[NUM_OF_RESULTS];
        panels = new JPanel[NUM_OF_RESULTS];
        boxes = new JCheckBox[NUM_OF_RESULTS];

        for (int i = 0; i < NUM_OF_RESULTS; i++) {

            String title = "Test Title " + i;
            String resume = "Test Resume " + i;

            titles[i] = new JLabel();
            boxes[i] = new JCheckBox();
            panels[i] = new JPanel();
            boxes[i].setPreferredSize(new Dimension(50, 50));
            panels[i].setPreferredSize(new Dimension(500, 50));
            panels[i].setLayout(new FlowLayout()); //FlowLayout is default for JPanel
            titles[i].setText(title);
            titles[i].setPreferredSize(new Dimension(500, 50));

            panels[i].add(boxes[i]);
            panels[i].add(titles[i]);
            bigPanel.add(panels[i], i, 0);
        }
    }

    public static void main(String args[]) {
        new Test();
    }
}

I'm trying to create a form for a Task List, that will have a list of tasks sitting in a JScrollPane, with some buttons below it, to essentially look like this:

enter image description here

The problem I'm running into is that the scroll pane seems to be taking over the whole frame and I can't see the buttons. I'm working on a simpler prototype to try to figure out what I'm doing wrong, but nothing I've seems to work.

Can someone please help me to contain the size of the JSCrollPane?

public class Test extends JFrame {

    private JLabel label1 = new JLabel();
    private JLabel label2 = new JLabel();
    private JLabel[] titles;
    private JLabel[] descriptions;
    private JPanel[] panels;
    private JCheckBox[] boxes;
    private JScrollPane jScrollPane1;
    private JPanel bigPanel;
    private final static int NUM_OF_RESULTS = 10;

    public Test() {

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(1000, 600));

        bigPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        //set layout to
        GridLayout layout = new GridLayout(30, 30);
        bigPanel.setLayout(layout);
        jScrollPane1 = new JScrollPane(bigPanel);
        jScrollPane1.setPreferredSize(new Dimension(500, 300));
        getContentPane().add(jScrollPane1);

        // How do I get these buttons to show on the main panel?
        // JButton button1 = new JButton("Save");
        // JButton button1 = new JButton("Cancel");
        // getContentPane.add(button1);
        // getContentPane.add(button2);

        scrollPanelDemo();
        pack();
        setVisible(true);
    }

    public void scrollPanelDemo() {

        titles = new JLabel[NUM_OF_RESULTS];
        descriptions = new JLabel[NUM_OF_RESULTS];
        panels = new JPanel[NUM_OF_RESULTS];
        boxes = new JCheckBox[NUM_OF_RESULTS];

        for (int i = 0; i < NUM_OF_RESULTS; i++) {

            String title = "Test Title " + i;
            String resume = "Test Resume " + i;

            titles[i] = new JLabel();
            boxes[i] = new JCheckBox();
            panels[i] = new JPanel();
            boxes[i].setPreferredSize(new Dimension(50, 50));
            panels[i].setPreferredSize(new Dimension(500, 50));
            panels[i].setLayout(new FlowLayout()); //FlowLayout is default for JPanel
            titles[i].setText(title);
            titles[i].setPreferredSize(new Dimension(500, 50));

            panels[i].add(boxes[i]);
            panels[i].add(titles[i]);
            bigPanel.add(panels[i], i, 0);
        }
    }

    public static void main(String args[]) {
        new Test();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文