如何使 GridBagLayout 中的中间列大于侧列?
How to get middle column bigger than side columns in GridBagLayout?
我正在尝试进行 3 列布局。我怎样才能让中间的列比侧列大?我正在使用来自 Java Swing 的 GridBagLayout
。我在摆弄 GridWidth
但它不起作用。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GridBag extends JFrame{
JPanel j1, j2, j3;
GridBagConstraints gbc = new GridBagConstraints();
public GridBag() {
super("Pandemic");
setSize(1000, 500); //suggested size
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridBagLayout());
j1 = new JPanel();
j1.setBackground(Color.blue);
j2 = new JPanel();
j2.setBackground(Color.red);
j3 = new JPanel();
j3.setBackground(Color.yellow);
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 1000;
this.add(j1, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
gbc.gridwidth = 2;
this.add(j2, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
this.add(j3, gbc);
this.setVisible(true);
}
public static void main(String[] args) {
new GridBag();
}
//end main
}
I was messing around with the gridwidth but it was not working.
您不能随便给组件一个随机数的网格宽度。一行中只有 3 个组件,因此每个组件的宽度只能为 1。
但是,如果第二行有两个分量,则可以为其中一个分量指定宽度 2,另一个分量宽度为 1。
How can I get the middle column bigger then side columns ?
gbc.weightx = 0.5;
上面指定了当框架的大小大于每个组件的首选大小时,如何将额外 space 分配给每个组件。
如果您不更改约束,则相同的值将用于所有组件(因为您使用相同的 GridBagConstraints 对象)。
在您的示例中,所有组件都使用相同的“weightx”,因此所有组件的大小都相同。
这是您需要更改的约束。如果您希望组件具有不同的大小,则它需要对您的组件有所不同。
尝试使用:
gbc.weightx = 0.25;
对于蓝色和黄色分量。现在红色部分大约是原来的两倍。
gbc.ipady = 1000;
您可能应该使用“填充”约束来填充每个面板的垂直高度。然后它将自动增长以填充框架中可用的 space。
阅读 How to Use GridBagLayout 上的 Swing 教程以获取更多信息和示例。
由于 JFrame
中的三个 JPanel
中的 none 包含任何组件,您可以简单地为每个组件设置首选大小,因为 GridBagLayout
尊重它正在管理其布局的组件的首选大小。这是一个example
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagExample {
private void createAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.25d;
gbc.weighty = 0.25d;
JPanel j1 = new JPanel();
j1.setPreferredSize(new Dimension(250, 400));
j1.setBackground(Color.blue);
frame.add(j1, gbc);
gbc.gridx = 1;
gbc.weightx = 0.5d;
gbc.weighty = 0.5d;
JPanel j2 = new JPanel();
j2.setPreferredSize(new Dimension(500, 400));
j2.setBackground(Color.red);
frame.add(j2, gbc);
gbc.gridx = 2;
gbc.weightx = 0.25d;
gbc.weighty = 0.25d;
JPanel j3 = new JPanel();
j3.setPreferredSize(new Dimension(250, 400));
j3.setBackground(Color.yellow);
frame.add(j3, gbc);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new GridBagExample().createAndDisplayGui());
}
}
这是 JFrame
的样子。
我正在尝试进行 3 列布局。我怎样才能让中间的列比侧列大?我正在使用来自 Java Swing 的 GridBagLayout
。我在摆弄 GridWidth
但它不起作用。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GridBag extends JFrame{
JPanel j1, j2, j3;
GridBagConstraints gbc = new GridBagConstraints();
public GridBag() {
super("Pandemic");
setSize(1000, 500); //suggested size
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridBagLayout());
j1 = new JPanel();
j1.setBackground(Color.blue);
j2 = new JPanel();
j2.setBackground(Color.red);
j3 = new JPanel();
j3.setBackground(Color.yellow);
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 1000;
this.add(j1, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
gbc.gridwidth = 2;
this.add(j2, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
this.add(j3, gbc);
this.setVisible(true);
}
public static void main(String[] args) {
new GridBag();
}
//end main
}
I was messing around with the gridwidth but it was not working.
您不能随便给组件一个随机数的网格宽度。一行中只有 3 个组件,因此每个组件的宽度只能为 1。
但是,如果第二行有两个分量,则可以为其中一个分量指定宽度 2,另一个分量宽度为 1。
How can I get the middle column bigger then side columns ?
gbc.weightx = 0.5;
上面指定了当框架的大小大于每个组件的首选大小时,如何将额外 space 分配给每个组件。
如果您不更改约束,则相同的值将用于所有组件(因为您使用相同的 GridBagConstraints 对象)。
在您的示例中,所有组件都使用相同的“weightx”,因此所有组件的大小都相同。
这是您需要更改的约束。如果您希望组件具有不同的大小,则它需要对您的组件有所不同。
尝试使用:
gbc.weightx = 0.25;
对于蓝色和黄色分量。现在红色部分大约是原来的两倍。
gbc.ipady = 1000;
您可能应该使用“填充”约束来填充每个面板的垂直高度。然后它将自动增长以填充框架中可用的 space。
阅读 How to Use GridBagLayout 上的 Swing 教程以获取更多信息和示例。
由于 JFrame
中的三个 JPanel
中的 none 包含任何组件,您可以简单地为每个组件设置首选大小,因为 GridBagLayout
尊重它正在管理其布局的组件的首选大小。这是一个example
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagExample {
private void createAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.25d;
gbc.weighty = 0.25d;
JPanel j1 = new JPanel();
j1.setPreferredSize(new Dimension(250, 400));
j1.setBackground(Color.blue);
frame.add(j1, gbc);
gbc.gridx = 1;
gbc.weightx = 0.5d;
gbc.weighty = 0.5d;
JPanel j2 = new JPanel();
j2.setPreferredSize(new Dimension(500, 400));
j2.setBackground(Color.red);
frame.add(j2, gbc);
gbc.gridx = 2;
gbc.weightx = 0.25d;
gbc.weighty = 0.25d;
JPanel j3 = new JPanel();
j3.setPreferredSize(new Dimension(250, 400));
j3.setBackground(Color.yellow);
frame.add(j3, gbc);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new GridBagExample().createAndDisplayGui());
}
}
这是 JFrame
的样子。