1

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

我有这个工作,但是,我也希望水平拆分窗格在窗口最大化时更改其大小。(目前没有。)

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

我怎样才能做到这一点?

谢谢!

以下是我目前正在使用的代码。我在控制器中调用 create 方法。首先调用 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()在发生这种情况时调用该方法,这是唯一的方法还是会起作用?(刚刚测试过这个,它没有用......只是把所有东西都恢复到首选尺寸。duh)


初看。 在此处输入图像描述


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

4

1 回答 1

4

你可能会看setResizeWeight();的值0.5应该均匀分布空间。

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

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

于 2011-10-19T04:53:40.537 回答