我已经编写了一个简单的swing gui。但是,我的问题是我的桌子在页面的底部。我想在按钮下方留出一些空间,并在底部留出更多空间。这是一个简短的程序,我在做什么:

public class minimumExample extends JFrame {

    private JTabbedPane tabbedPane;
    private FilteredTabPanel filteredTabPanel;

    public void createTabBar() {

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        filteredTabPanel = new FilteredTabPanel();
        tabbedPane.addTab("Test", filteredTabPanel.createLayout());

        add(tabbedPane);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    private void makeLayout() {

        setTitle("Test App");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));
        createTabBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }

    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();
            }
        });
    }

    public static void main(String[] args) throws IOException {
        minimumExample ex = new minimumExample();
        ex.start();
    }

    public class FilteredTabPanel extends JPanel {

        private JPanel selectionArea;
        private JLabel lCity;
        private JComboBox cityBox;
        private JTable filterTable;
        String[] columnNames = {"Cities"};
        String[][] data = {
                {"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"},{"Columbia"},{"DC"},{"DC"},{"DC"},{"DC"},{"DC"},{"DC"}
        };

        private JScrollPane scrollPane;

        public JPanel createLayout() {
            JPanel panel = new JPanel(new GridLayout(0, 1));
            //add panels to the layout
            panel.add(addButtons());
            panel.add(showTable());

            repaint();
            revalidate();

            return panel;
        }

        public JPanel addButtons(){

            selectionArea = new JPanel(new FlowLayout(FlowLayout.LEFT));

            lCity = new JLabel("City");

            String[] fillings = {"NY", "LA", "Columbia", "DC"};
            cityBox = new JComboBox(fillings);

            cityBox.addActionListener(new ActionListener() {

                private String cityFilter;

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    //2. get data
                    cityFilter = cityBox.getSelectedItem().toString();
                }
            });

            selectionArea.add(lCity);
            selectionArea.add(cityBox);

            selectionArea.repaint();

            return selectionArea;
        }

        private JScrollPane showTable() {

            filterTable =new JTable(data, columnNames);
            filterTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            scrollPane = new JScrollPane(filterTable);

            scrollPane.repaint();
            scrollPane.validate();

            return scrollPane;
        }
    }
}


页面外观如下:



任何建议如何解决?

最佳答案

您想要做的是嵌套简单的布局管理器
创建您的布局。我认为官方的Swing教程
通过引入这些对程序员造成伤害
首先是简单的布局管理器。我建议避免它们,
在更强大的经理身上花一些时间。
FlowLayoutBorderLayoutGridlayout真的非常
简单的经理,但他们做不到。

我建议使用以下两个管理器:


MigLayout
GroupLayout


(JGoodies的FormLayout也是一个不错的选择。)

这些经理不仅更强大,而且还可以应付
在其他失败的情况下具有更高级的要求。 (解析度
独立性,遵守OS设计原则等)

在介绍两个代码示例之前,我想说一些
指向您的代码。您不必要呼叫repaint()
revalidate()方法。

setPreferredSize(new Dimension(1000, 500));


以像素为单位指定大小不可移植。你最好依靠
pack()方法。

setLayout(new BorderLayout());


JFrame's默认布局管理器(更确切地说是其内容窗格的)
BorderLayout。因此,不需要此行。

以下两个示例是使用MigLayoutGroupLayout的解决方案。

MigLayout解决方案

MigLayout是第三方布局管理器,因此您需要下载和
向您的项目添加其他jar。

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import net.miginfocom.swing.MigLayout;

public class MigLayoutEx extends JFrame {

    private JComboBox cityBox;
    private JTable filterTable;

    public MigLayoutEx() {

        initUI();
    }

    private void initUI() {

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        add(tabbedPane);

        filterTable = createTable();
        cityBox = createCityBox();

        JPanel pnl = new JPanel(new MigLayout(""));
        pnl.add(new JLabel("City"), "split 2");
        pnl.add(cityBox, "wrap");

        pnl.add(new JScrollPane(filterTable));

        tabbedPane.addTab("Test", pnl);
        pack();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private JTable createTable() {

        String[] columnNames = {"Cities"};
        String[][] data = {
            {"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"}, {"Columbia"},
            {"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}
        };

        JTable table = new JTable(data, columnNames);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        return table;
    }

    private JComboBox createCityBox() {

        String[] fillings = {"NY", "LA", "Columbia", "DC"};
        JComboBox box = new JComboBox(fillings);

        return box;
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutEx ex = new MigLayoutEx();
                ex.setVisible(true);
            }
        });
    }
}




GroupLayout解决方案

GroupLayout是内置的布局管理器,无需添加其他jar。

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

public class GroupLayoutEx extends JFrame {

    private JComboBox cityBox;
    private JTable filterTable;

    public GroupLayoutEx() {

        initUI();
    }

    private void initUI() {

        JPanel pnl = new JPanel();
        GroupLayout gl = new GroupLayout(pnl);
        pnl.setLayout(gl);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        add(tabbedPane);

        JLabel cityLbl = new JLabel("City");
        filterTable = createTable();
        JScrollPane spane = new JScrollPane(filterTable);
        cityBox = createCityBox();

        gl.setAutoCreateGaps(true);
        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addGroup(gl.createSequentialGroup()
                        .addComponent(cityLbl)
                        .addComponent(cityBox))
                .addComponent(spane)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(cityLbl)
                        .addComponent(cityBox))
                .addComponent(spane)
        );

        tabbedPane.addTab("Test", pnl);
        pack();

        setTitle("GroupLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private JTable createTable() {

        String[] columnNames = {"Cities"};
        String[][] data = {
            {"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"}, {"Columbia"},
            {"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}, {"DC"}
        };

        JTable table = new JTable(data, columnNames);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        return table;
    }

    private JComboBox createCityBox() {

        String[] fillings = {"NY", "LA", "Columbia", "DC"};
        JComboBox box = new JComboBox(fillings);

        return box;
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GroupLayoutEx ex = new GroupLayoutEx();
                ex.setVisible(true);
            }
        });
    }
}


我建议同时研究经理和选择您喜欢的人。

10-08 11:04