水平拆分窗格使用窗口调整大小

时间:2011-10-19 02:18:41

标签: java swing user-interface jsplitpane

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

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

我还在水平窗格下方有一个按钮框,并希望在调整窗口大小时它始终可见。目前,当窗口启动时,我可以看到水平分割中的所有内容。我无法看到按钮,因为它们不适合窗口的首选大小(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()方法,这是唯一的方式还是会工作? (刚试过这个,它没有用......只是把所有东西都放回到首选尺寸。呃)


初看。 enter image description here


窗口最大化后。 enter image description here

1 个答案:

答案 0 :(得分:4)

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

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

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