![](/img/trans.png)
[英]How does gridwidth and gridheight work (Java guid GridBagLayout)?
[英]GridBagLayout gridheight constraint not affecting result
我正在使用GridBag布局進行Java Swing項目。 我正在嘗試制作兩個寬度相同且水平對齊但高度不同的面板。
我有以下代碼:
import javax.swing.*;
import java.awt.*;
public class Mega extends JFrame {
public static void main(String[] args) {
new Mega();
}
public Mega() {
Dimension minDimension = new Dimension();
minDimension.width = 800;
minDimension.height = 800;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
this.add(mainPanel);
GridBagConstraints constraints = new GridBagConstraints();
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.GRAY);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.fill = GridBagConstraints.BOTH;
mainPanel.add(topPanel, constraints);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.GREEN);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 3;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.fill = GridBagConstraints.BOTH;
mainPanel.add(bottomPanel, constraints);
this.setVisible(true);
}
}
使用此代碼,底部面板的高度應該是第一行的三倍,因為它占據了三行,而頂部行僅占據了一行。 但是,我得到的是這樣的。
看起來gridheight = 3約束沒有什么不同,因為兩個面板的高度相同。 我究竟做錯了什么?
gridHeight
不會有任何影響,因為沒有更多行可以擴展(將gridWidth
和gridHeight
視為“擴展”),而是從組件中提供更好的尺寸提示或使用weighty
屬性
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Mega extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Mega();
}
});
}
public Mega() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel() {
// This is done for demonstration purposes
// it would be better for the child components
// to provide appropriate sizing hints
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
};
mainPanel.setLayout(new GridBagLayout());
this.add(mainPanel);
GridBagConstraints constraints = new GridBagConstraints();
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.GRAY);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1;
constraints.weighty = 0.25;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5, 5, 5, 5);
mainPanel.add(topPanel, constraints);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.GREEN);
constraints.gridy = 1;
constraints.weighty = 0.75;
mainPanel.add(bottomPanel, constraints);
pack();
setLocationRelativeTo(null);
this.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.