代码之家  ›  专栏  ›  技术社区  ›  Grains

用java创建一个自定义表

  •  0
  • Grains  · 技术社区  · 11 年前

    我想创建一个这样的表,但标题与表同步:

    enter image description here

    关于如何使列与行对齐并允许在x轴上自动调整大小,有什么技巧吗?为此,我使用了 GridBagLayout 。也尝试过 JTable 但在自定义它以隐藏所有默认值时出现问题 JTable(JT表格) 组件(如我的图片)。

    public class MyTableExample extends JFrame {
    
        public MyTableExample(){
            JPanel contentPanel = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.weightx = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            contentPanel.add(getTitles(),c);
            c.gridy = 2;
            contentPanel.add(getRow(),c);
            c.gridy = 3;
            contentPanel.add(getRow(),c);
    
            // MainPanel: Helps positioning the contentPanel
            JPanel mainPanel = new JPanel(new GridBagLayout());
            c.weighty = 1;
            c.anchor = GridBagConstraints.NORTH;
            c.insets = new Insets(20,20,20,20);
            mainPanel.add(contentPanel,c);
    
            this.add(mainPanel);
            this.pack();
            this.setVisible(true);
        }
    
        private JPanel getRow(){
            JPanel rowPanel = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.weightx = 1;
            c.weighty = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.WEST;
    
            rowPanel.setOpaque(false);
            JLabel col1 = new JLabel("#rowItem");
            JComboBox<String> col2 = new JComboBox<String>();
            JButton col3 = new JButton("rowItem");
            JLabel col4 = new JLabel("rowItem");
            rowPanel.add(col1,c);
            rowPanel.add(col2,c);
            rowPanel.add(col3,c);
            rowPanel.add(col4,c);
    
            return rowPanel;
        }
    
        private JPanel getTitles(){
            JPanel titlePanel = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.weightx = 1;
            c.weighty = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.WEST;
    
            titlePanel.setOpaque(false);
            JLabel t1 = new JLabel("T1");
            JLabel t2 = new JLabel("Title2");
            JLabel t3 = new JLabel("Title33333333");
            JLabel t4 = new JLabel("T4");
            titlePanel.add(t1,c);
            titlePanel.add(t2,c);
            titlePanel.add(t3,c);
            titlePanel.add(t4,c);
    
            return titlePanel;
        }
    
        public static void main(String[] args){
            new MyTableExample();
        }
    }
    
    2 回复  |  直到 11 年前
        1
  •  2
  •   Eric Stein    11 年前

    如果你同意所有的细胞都是相同的大小, GridLayout 这将是一个简单的解决方案。我在新布局的可用性方面落后于时代,这可能会有所帮助,但GridBag可以有一点学习曲线。如果你想让所有东西都排列整齐,你需要有一个GridBagLayout,而不是每行一个。

        2
  •  1
  •   JB Nizet    11 年前

    使用一个面板,并使用GridBagLayout将所有组件直接放置在此面板中。这样,所有组件都将被放置在一个网格中,从而正确对齐。