gridlayout布局(gridlayout布局计算器)
android tablelayout布局和gridlayout布局的区别 简单点
tablelayout:排版和数据联动,不支持元素合并。
gridlayout:可自由指定行列和对齐方式,使用更灵活。
简单展示数据可用tablelayout,否则用gridlayout。
JAVA中GridLayout的布局问题 如图,如何弄成这样的布局,
public class NewJFrame1 extends javax.swing.JFrame {
/** Creates new form NewJFrame1 */
public NewJFrame1() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N
getContentPane().setLayout(new java.awt.GridBagLayout());
jScrollPane1.setName("jScrollPane1"); // NOI18N
jTree1.setName("jTree1"); // NOI18N
jScrollPane1.setViewportView(jTree1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
jLabel1.setText(org.openide.util.NbBundle.getMessage(NewJFrame1.class, "NewJFrame1.jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jLabel1, gridBagConstraints);
jLabel2.setText(org.openide.util.NbBundle.getMessage(NewJFrame1.class, "NewJFrame1.jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jLabel2, gridBagConstraints);
pack();
}// /editor-fold
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration
}
关于java中GridLayout布局的一个小问题,肯定大家帮忙解释,谢谢...
GRID布局的源码是这么进行计算的:
if
(nrows
0)
ncols
=
(ncomponents
+
nrows
-
1)
/
nrows;
nrows就是你设定的行数,ncomponents是你添加的组件数目,这儿就是16,ncols就是列数。
使用(4,3)和(4,5)时,列数
=
(16
+
4
-1)/4
=
4
使用(3,4)时,列数
=
(16
+
3
-1)/3
=
6
明白了么?
gridlayout布局的三种构造方法
GridLayout() 一行一列
GridLayout(int rows, int cols) rows行 cols列
GridLayout(int rows, int cols, int hgap, int vgap) rows行 cols 列, 列间距hgap, 行间距vgap