⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridbagutils.java

📁 java 表格排序
💻 JAVA
字号:
/*
 * GridBagUtils.java
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package au.id.dodson.FilterableSortableTable;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class GridBagUtils extends JFrame {
    boolean inAnApplet = true;
    final boolean shouldFill = true;
    final boolean shouldWeightX = true;
    
    public GridBagUtils() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (inAnApplet) {
                    dispose();
                } else {
                    System.exit(0);
                }
            }
        });
    }
    
    /**
     *
     * @param rows
     * @return
     */
    public static JComponent layoutInGrid(Vector rows) {
        return layoutInGrid(rows, false);
    }
    
    /**
     *
     * @param rows
     * @param scroll
     * @return
     */
    public static JComponent layoutInGrid(Vector rows, boolean scroll) {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(1,1,1,1);
        JPanel panel = new JPanel(gridbag);
        int row = 0;
        Iterator rowItr = rows.iterator();
        while (rowItr.hasNext()) {
            Vector cols = (Vector)rowItr.next();
            int col = 0;
            Component componentPrevious = null;
            Iterator colItr = cols.iterator();
            while (colItr.hasNext()) {
                Component component = (Component)colItr.next();
                if (component == null) {
                    component = new JPanel();
                }
                if (component == componentPrevious) {
                    c.gridwidth++;
                } else {
                    c.gridy = row;
                    c.gridx = col;
                    c.gridwidth = 1;
                    panel.add(component);
                }
                gridbag.setConstraints(component, c);
                componentPrevious = component;
                col++;
            }
            row++;
        }
        if (scroll) {
            return new JScrollPane(panel);
        } else {
            return panel;
        }
    }
    
    /**
     *
     * @param args
     */
//    public static void main(String args[]) {
//        GridBagUtils window = new GridBagUtils();
//        JButton button;
//        Vector iv = new Vector();
//        for (int i = 0; i<6; i++) {
//            Vector jv = new Vector();
//            for (int j = 0; j<4; j++) {
//                JButton b = new JButton("Button " + i + j);
//                jv.add(b);
//            }
//            iv.add(jv);
//        }
//        window.getContentPane().add(layoutInGrid(iv));
//        window.inAnApplet = false;
//        
//        window.setTitle("GridBagLayout");
//        window.pack();
//        window.setVisible(true);
//    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -