水平分割窗格随窗口调整大小

发布于 2024-12-10 23:14:21 字数 6162 浏览 0 评论 0原文

我有一个水平分割窗格,其中包含一个垂直分割窗格。当窗口显示时,我希望水平分割窗格顶部的垂直分割窗格在中间分割。我希望水平分隔线位于中间。

我已经完成了这项工作,但是,我还希望水平分割窗格在窗口最大化时更改其大小。 (目前没有。)

我在水平窗格下方还有一个按钮框,并希望在调整窗口大小时它始终可见。目前,当窗口启动时,我可以看到水平分割中的所有内容。我看不到按钮,因为它们不适合窗口的首选大小(800、600)。但我希望所有内容都能自动显示在窗口中,并在调整大小时保持粘在窗口边框上...

我该怎么做?

谢谢!

下面是我当前使用的代码。我在控制器中调用创建方法。首先调用 createView,然后按顺序调用其余部分。


public void createView() {
        dialog = new JFrame();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
        dialog.setBounds(0, 0, 800, 600);
        dialog.setMinimumSize(new Dimension(800, 600));
        dialog.setPreferredSize(new Dimension(800, 600));
        dialog.setResizable(true);
        dialog.setTitle("MJLA Class Control Panel");

        contentPanel = new JPanel();
//      contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);

        classQuizSRTSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        contentPanel.add(classQuizSRTSplit, BorderLayout.NORTH);

        classQuizSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        classQuizSRTSplit.setTopComponent(classQuizSplit);




//      classQuizHBox = Box.createHorizontalBox();
//      contentPanel.add(classQuizHBox);

        sRTHBox = Box.createHorizontalBox();
        contentPanel.add(sRTHBox);

        buttonBox = Box.createHorizontalBox();
        contentPanel.add(buttonBox, BorderLayout.SOUTH);

        refreshButton = new JButton("Refresh");
        buttonBox.add(refreshButton);

        doneButton = new JButton("Done");
        buttonBox.add(doneButton);

        this.validateView();
    }

    public void createClassTablePanel() {
        this.classTablePanel = new JPanel();
        this.classTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.classTablePanel.setPreferredSize(new Dimension(300, 300));
        this.classTablePanel.setLayout(new BorderLayout());

//      this.classQuizHBox.add(classTablePanel);

        this.classQuizSplit.setLeftComponent(classTablePanel);
        classTableModel = cPModel.getClassTableModel();

        classTable = new JTable(this.classTableModel);

        classTable.getSelectionModel().addListSelectionListener(this);
        JScrollPane scrollPane = new JScrollPane(classTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        classTablePanel.add(scrollPane, BorderLayout.CENTER);

        this.validateView();
    }

    public void createQuizTablePanel() {
        this.quizTablePanel = new JPanel();
        this.quizTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.quizTablePanel.setPreferredSize(new Dimension(300, 300));
        this.quizTablePanel.setLayout(new BorderLayout());

//      this.classQuizHBox.add(quizTablePanel);

        this.classQuizSplit.setRightComponent(quizTablePanel);

        quizTableModel = cPModel.getQuizTableModel();
        this.quizSorter = new TableRowSorter<DefaultTableModel>(quizTableModel);

        quizTable = new JTable(this.quizTableModel);
        quizTable.getSelectionModel().addListSelectionListener(this);
        quizTable.setRowSorter(quizSorter);
        JScrollPane scrollPane = new JScrollPane(quizTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        quizTablePanel.add(scrollPane, BorderLayout.CENTER);

        Box buttonHBox = Box.createHorizontalBox();
        quizTablePanel.add(buttonHBox, BorderLayout.SOUTH);

        addQuizButton = new JButton("Add Quiz");
        buttonHBox.add(addQuizButton);

        removeQuizButton = new JButton("Remove Quiz");
        buttonHBox.add(removeQuizButton);

        editQuizButton = new JButton("Edit Quiz");
        buttonHBox.add(editQuizButton);

        this.validateView();
    }

    public void createStudentRecordTablePanel() {
        this.studentRecordTablePanel = new JPanel();
        this.studentRecordTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.studentRecordTablePanel.setLayout(new BorderLayout());

//      this.sRTHBox.add(studentRecordTablePanel);

        this.classQuizSRTSplit.setBottomComponent(studentRecordTablePanel);

        this.studentRecordTableModel = cPModel.getStudentRecordTableModel();
        this.sRTSorter = new TableRowSorter<DefaultTableModel>(studentRecordTableModel);

        sRTTable = new JTable(this.studentRecordTableModel);
        sRTTable.setRowSorter(sRTSorter);
        JScrollPane scrollPane = new JScrollPane(sRTTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        studentRecordTablePanel.add(scrollPane, BorderLayout.CENTER);

        Box buttonHBox = Box.createHorizontalBox();
        studentRecordTablePanel.add(buttonHBox, BorderLayout.SOUTH);

        editGradeButton = new JButton("Edit Grade");
        buttonHBox.add(editGradeButton);

        generateReportButton = new JButton("Generate Report");
        buttonHBox.add(generateReportButton);

        this.validateView();
    }

另一个问题。

这解决了其中一个问题@TrashGod。但是,如何使水平分割窗格调整其组件大小以适应窗口的新大小,而不是完成和刷新按钮与水平分割窗格底部之间的巨大间隙?

我在想,当窗口大小发生变化时,我必须监听一个事件,然后在发生这种情况时调用 pack() 方法,这是唯一的方法吗? (刚刚测试过这个,它不起作用......只是将所有内容恢复到首选尺寸。呃)


初始外观。 在此处输入图像描述


窗口最大化后。 在此处输入图像描述

I have a horizontal split pane that contains a vertical split pane. When the window shows up i want the vertical split pane in the top of the horizontal split pane to be split in the middle. I want the horizontal divider to be in the middle.

I have that working, however, i also want to the horizontal split pane to change its size when the window is maximized. (It currently does not.)

I also have a button box below the horizontal pane and would like it to always be visible when the window is resized. Currently when the window launches i can see everything in the horizontal split. I am unable to see the buttons, because they do not fit in the preferred size of the window (800, 600). But i would like everything to show up in the window automatically and stay Glue'd to the border of the window when it is resized...

How can i do this?

Thanks!

Below is the code i am currently using. I call the create methods in a controller. createView is called first then the rest in sequential order.


public void createView() {
        dialog = new JFrame();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
        dialog.setBounds(0, 0, 800, 600);
        dialog.setMinimumSize(new Dimension(800, 600));
        dialog.setPreferredSize(new Dimension(800, 600));
        dialog.setResizable(true);
        dialog.setTitle("MJLA Class Control Panel");

        contentPanel = new JPanel();
//      contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);

        classQuizSRTSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        contentPanel.add(classQuizSRTSplit, BorderLayout.NORTH);

        classQuizSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        classQuizSRTSplit.setTopComponent(classQuizSplit);




//      classQuizHBox = Box.createHorizontalBox();
//      contentPanel.add(classQuizHBox);

        sRTHBox = Box.createHorizontalBox();
        contentPanel.add(sRTHBox);

        buttonBox = Box.createHorizontalBox();
        contentPanel.add(buttonBox, BorderLayout.SOUTH);

        refreshButton = new JButton("Refresh");
        buttonBox.add(refreshButton);

        doneButton = new JButton("Done");
        buttonBox.add(doneButton);

        this.validateView();
    }

    public void createClassTablePanel() {
        this.classTablePanel = new JPanel();
        this.classTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.classTablePanel.setPreferredSize(new Dimension(300, 300));
        this.classTablePanel.setLayout(new BorderLayout());

//      this.classQuizHBox.add(classTablePanel);

        this.classQuizSplit.setLeftComponent(classTablePanel);
        classTableModel = cPModel.getClassTableModel();

        classTable = new JTable(this.classTableModel);

        classTable.getSelectionModel().addListSelectionListener(this);
        JScrollPane scrollPane = new JScrollPane(classTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        classTablePanel.add(scrollPane, BorderLayout.CENTER);

        this.validateView();
    }

    public void createQuizTablePanel() {
        this.quizTablePanel = new JPanel();
        this.quizTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.quizTablePanel.setPreferredSize(new Dimension(300, 300));
        this.quizTablePanel.setLayout(new BorderLayout());

//      this.classQuizHBox.add(quizTablePanel);

        this.classQuizSplit.setRightComponent(quizTablePanel);

        quizTableModel = cPModel.getQuizTableModel();
        this.quizSorter = new TableRowSorter<DefaultTableModel>(quizTableModel);

        quizTable = new JTable(this.quizTableModel);
        quizTable.getSelectionModel().addListSelectionListener(this);
        quizTable.setRowSorter(quizSorter);
        JScrollPane scrollPane = new JScrollPane(quizTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        quizTablePanel.add(scrollPane, BorderLayout.CENTER);

        Box buttonHBox = Box.createHorizontalBox();
        quizTablePanel.add(buttonHBox, BorderLayout.SOUTH);

        addQuizButton = new JButton("Add Quiz");
        buttonHBox.add(addQuizButton);

        removeQuizButton = new JButton("Remove Quiz");
        buttonHBox.add(removeQuizButton);

        editQuizButton = new JButton("Edit Quiz");
        buttonHBox.add(editQuizButton);

        this.validateView();
    }

    public void createStudentRecordTablePanel() {
        this.studentRecordTablePanel = new JPanel();
        this.studentRecordTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.studentRecordTablePanel.setLayout(new BorderLayout());

//      this.sRTHBox.add(studentRecordTablePanel);

        this.classQuizSRTSplit.setBottomComponent(studentRecordTablePanel);

        this.studentRecordTableModel = cPModel.getStudentRecordTableModel();
        this.sRTSorter = new TableRowSorter<DefaultTableModel>(studentRecordTableModel);

        sRTTable = new JTable(this.studentRecordTableModel);
        sRTTable.setRowSorter(sRTSorter);
        JScrollPane scrollPane = new JScrollPane(sRTTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        studentRecordTablePanel.add(scrollPane, BorderLayout.CENTER);

        Box buttonHBox = Box.createHorizontalBox();
        studentRecordTablePanel.add(buttonHBox, BorderLayout.SOUTH);

        editGradeButton = new JButton("Edit Grade");
        buttonHBox.add(editGradeButton);

        generateReportButton = new JButton("Generate Report");
        buttonHBox.add(generateReportButton);

        this.validateView();
    }

Another issue.

That fixed one of the problems @TrashGod. However, how can i make the horizontal split pane resize its component to fit the new size of the window, instead of their being that big gap between the done and refresh button and the bottom of the horizontal split pane?

I was thinking that i would have to listen for an event for when the window size changes and then call the pack() method when that happens, is that the only way or would that even work? (Just tested this, it did not work... just puts everything back to preferred sizes. duh)


Initial look.
enter image description here


After window maximized.
enter image description here

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2024-12-17 23:14:21

您可以查看 setResizeWeight();值为 0.5 应该均匀分配空间。

pack() 方法“使此Window的大小适应其子组件的首选大小和布局。” BorderLayout.NORTHBorderLayout.SOUTH 似乎是适合与分隔线保持一致的布局。

如需其他帮助,请提供显示该问题的 sscce

You might look at setResizeWeight(); a value of 0.5 should distribute the space evenly.

The pack() method "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." BorderLayout.NORTH and BorderLayout.SOUTH seem like suitable layouts for staying with the divider.

For additional help, please provide an sscce that exhibits the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文