[英]How to align components center in the JPanel using GridBagLayout?
[英]How to push GridbagLayout not to lay components in the center of JPanel
問題在於組件的居中布局,GridBagLayout 始終“位於”JPanel 的中心,所以我不在乎它將如何在內部布局組件,我的問題是這些組件將在面板上的何處開始布局。
我試過:
panel.setAlignmentX( JPanel.LEFT_ALIGNMENT );
但它沒有幫助。
任何的想法?
您需要添加至少一個將填充水平空間的組件。 如果你沒有這樣的組件,你可以試試這個:
GridBagConstraints noFill = new GridBagConstraints();
noFill.anchor = GridBagConstraints.WEST;
noFill.fill = GridBagConstraints.NONE;
GridBagConstraints horizontalFill = new GridBagConstraints();
horizontalFill.anchor = GridBagConstraints.WEST;
horizontalFill.fill = GridBagConstraints.HORIZONTAL;
panel.add(new JLabel("Left Aligned"), noFill);
panel.add(Box.createHorizontalGlue(), horizontalFill);
除了設置anchor
和fill
字段外,您可能還需要設置weightx
字段。 這有助於指定調整大小的行為。
報價:
除非您為 weightx 或 weighty 指定至少一個非零值,否則所有組件都會聚集在其容器的中心。 這是因為當權重為 0.0(默認值)時,GridBagLayout 在其單元格網格和容器邊緣之間放置任何額外空間。
以下將保持myComponent
錨定到NORTHWEST
角。 假設this
是JPanel
或類似的:
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Specify horizontal fill, with top-left corner anchoring
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTHWEST;
// Select x- and y-direction weight. Without a non-zero weight,
// the component will still be centered in the given direction.
c.weightx = 1;
c.weighty = 1;
// Add child component
add(myComponent, c);
要保持子組件左對齊但垂直居中,只需設置anchor = WEST
並刪除weighty = 1;
.
您只需使用此實用程序 jar painless-gridbag即可完成。 它還使您使用 GridBagLayout 的代碼更漂亮,如下所示
PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false);
gbl.row().cell(lblFirstName).cell(txtFirstName).fillX()
.cell(lblFamilyName).cell(txtFamilyName).fillX();
gbl.row().cell(lblAddress).cellXRemainder(txtAddress).fillX();
gbl.doneAndPushEverythingToTop();
如果要更改組件在GridBagLayout
創建的單元格中的位置,請使用GridBagConstraints 中的參數錨點。
我和你有同樣的問題。 通過將該面板添加到另一個具有 BorderLayout 和 NORTH 約束的面板中來解決它。
翁德雷
您可以將主布局設置為 flowlayout 並將對齊設置為左對齊。 在這個面板(flowlayout)中,添加一個gridbaglayout面板。 這也適用於 netbeans
另一種解決方案是在最右側,最底部添加兩個虛擬面板(容器)。 然后你調整 weightx 和 weighty 來分配額外的空間。 如果您將 1 設置為虛擬,那么所有額外的空間都分配給這個虛擬。
這是在netbeans 中形成的示例。
package tutorial;
/**
*
* @author ttn
*/
public class GridBag1 extends javax.swing.JPanel {
/**
* Creates new form GridBag1
*/
public GridBag1() {
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;
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setLayout(new java.awt.GridBagLayout());
jLabel1.setText("jLabel1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
add(jLabel1, gridBagConstraints);
jTextField1.setText("jTextField1");
jTextField1.setMinimumSize(new java.awt.Dimension(59, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 0.3;
add(jTextField1, gridBagConstraints);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 227, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 104, Short.MAX_VALUE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridheight = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
add(jPanel1, gridBagConstraints);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 172, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 196, Short.MAX_VALUE)
);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
add(jPanel2, gridBagConstraints);
jLabel2.setText("jLabel2");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
add(jLabel2, gridBagConstraints);
jTextField2.setText("jTextField2");
jTextField2.setMinimumSize(new java.awt.Dimension(59, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 0.3;
add(jTextField2, gridBagConstraints);
jScrollPane1.setMinimumSize(new java.awt.Dimension(104, 64));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
add(jScrollPane1, gridBagConstraints);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.