使用 GridBagLayout 将内容对齐到顶部
Align content to top with GridBagLayout
我正在使用 GridBagLayout 来放置面板,如下所示。我想将所有内容对齐到顶部。我可以通过在我的背景面板 (jp) 上使用 BorderLayout.NORTH 来管理它,但是背景面板不会填充 window
如何使用 GridBagLayout 将我的面板对齐到顶部?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
JPanel jp = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
public Test() {
jp1.setPreferredSize(new Dimension(100, 100));
jp2.setPreferredSize(new Dimension(100, 100));
jp3.setPreferredSize(new Dimension(100, 100));
jp4.setPreferredSize(new Dimension(100, 100));
jp1.setBackground(Color.red);
jp2.setBackground(Color.blue);
jp3.setBackground(Color.yellow);
jp4.setBackground(Color.green);
GridBagLayout g = new GridBagLayout();
jp.setLayout(g);
jp.setBackground(Color.BLACK);
GridBagConstraints con;
con = new GridBagConstraints();
con.insets = new Insets(3, 3, 3, 3); // top, left, bottom, right
// PANEL ONE
con.gridx = 0;
con.gridy = 0;
g.setConstraints(jp1, con);
jp.add(jp1);
// PANEL TWO
con.gridx = 1;
con.gridy = 0;
g.setConstraints(jp2, con);
jp.add(jp2);
// PANEL THREE
con.gridx = 0;
con.gridy = 1;
g.setConstraints(jp3, con);
jp.add(jp3);
// PANEL FOUR
con.gridx = 1;
con.gridy = 1;
g.setConstraints(jp4, con);
jp.add(jp4);
this.setSize(600, 300);
// I CAN MANAGE TO GET THE PANELS ALIGNED TO THE TOP BY USING BORDERLAYOUT
// LIKE BELOW BUT THEN THE BACKGROUNDPANEL DOESNT FILL THE WINDOW
// this.add(jp, BorderLayout.NORTH);
this.add(jp);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
but then the backgroundpanel doesnt fill the window
为什么要让 backgroundPanel 填充 window?
如果你只想要黑色背景,那么你可以这样做:
//this.add(jp);
this.getContentPane().setBackground( jp.getBackground() );
this.add(jp, BorderLayout.NORTH);
或者在使用 GridBagLayout 时需要指定 "anchor" 和 "weighty" 约束:
con.gridx = 0;
con.gridy = 0;
con.anchor = ...;
...
con.gridx = 1;
con.gridy = 1;
con.weighty = ...;
阅读有关 How to Use GridBagLayout 的 Swing 教程部分,了解这些约束的作用。我已经指出您需要使用哪些约束以及将它们包含在您的示例中的什么位置。
另一种方法是使用包装面板:
JPanel wrapper = new JPanel();
wrapper.setBackground( Color.BLACK );
wrapper.add( jp );
this.add( wrapper );
默认情况下,JPanel 使用 FlowLayout,它会考虑添加到面板的任何组件的大小。
我正在使用 GridBagLayout 来放置面板,如下所示。我想将所有内容对齐到顶部。我可以通过在我的背景面板 (jp) 上使用 BorderLayout.NORTH 来管理它,但是背景面板不会填充 window
如何使用 GridBagLayout 将我的面板对齐到顶部?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
JPanel jp = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
public Test() {
jp1.setPreferredSize(new Dimension(100, 100));
jp2.setPreferredSize(new Dimension(100, 100));
jp3.setPreferredSize(new Dimension(100, 100));
jp4.setPreferredSize(new Dimension(100, 100));
jp1.setBackground(Color.red);
jp2.setBackground(Color.blue);
jp3.setBackground(Color.yellow);
jp4.setBackground(Color.green);
GridBagLayout g = new GridBagLayout();
jp.setLayout(g);
jp.setBackground(Color.BLACK);
GridBagConstraints con;
con = new GridBagConstraints();
con.insets = new Insets(3, 3, 3, 3); // top, left, bottom, right
// PANEL ONE
con.gridx = 0;
con.gridy = 0;
g.setConstraints(jp1, con);
jp.add(jp1);
// PANEL TWO
con.gridx = 1;
con.gridy = 0;
g.setConstraints(jp2, con);
jp.add(jp2);
// PANEL THREE
con.gridx = 0;
con.gridy = 1;
g.setConstraints(jp3, con);
jp.add(jp3);
// PANEL FOUR
con.gridx = 1;
con.gridy = 1;
g.setConstraints(jp4, con);
jp.add(jp4);
this.setSize(600, 300);
// I CAN MANAGE TO GET THE PANELS ALIGNED TO THE TOP BY USING BORDERLAYOUT
// LIKE BELOW BUT THEN THE BACKGROUNDPANEL DOESNT FILL THE WINDOW
// this.add(jp, BorderLayout.NORTH);
this.add(jp);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
but then the backgroundpanel doesnt fill the window
为什么要让 backgroundPanel 填充 window?
如果你只想要黑色背景,那么你可以这样做:
//this.add(jp);
this.getContentPane().setBackground( jp.getBackground() );
this.add(jp, BorderLayout.NORTH);
或者在使用 GridBagLayout 时需要指定 "anchor" 和 "weighty" 约束:
con.gridx = 0;
con.gridy = 0;
con.anchor = ...;
...
con.gridx = 1;
con.gridy = 1;
con.weighty = ...;
阅读有关 How to Use GridBagLayout 的 Swing 教程部分,了解这些约束的作用。我已经指出您需要使用哪些约束以及将它们包含在您的示例中的什么位置。
另一种方法是使用包装面板:
JPanel wrapper = new JPanel();
wrapper.setBackground( Color.BLACK );
wrapper.add( jp );
this.add( wrapper );
默认情况下,JPanel 使用 FlowLayout,它会考虑添加到面板的任何组件的大小。