尝试使用 GridBagLayout 来管理 Java 中的 JPanel
Trying to use a GridBagLayout to manage JPanels in Java
我目前正在使用 GUI 迈出第一步,现在我只是想根据我的艺术作品制作一个 JFrame
和两个 JPanel
容器:
我选择 GridBagLayout
因为我希望它以后可以调整大小但保持给定的比例,但我很难理解所有这些是如何工作的。
我当前的代码如下所示:
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
private JPanel contentPane;
public JPanel getContentPane() {
return contentPane;
}
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
public MyFrame (){
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 3;
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
contentPane.add(blue, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 3;
JPanel gruen = new JPanel();
gruen.setBackground(Color.green);
contentPane.add(gruen, gbc);
this.setPreferredSize(new Dimension(630, 650));
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setContentPane(contentPane);
}
public static void main(String[] args) {
new MyFrame();
}
}
这给了我这个空 window:
如果有人能告诉我我做错了什么,那就太好了。
1.首先,你需要删除这个:
public JPanel getContentPane() {
return contentPane;
}
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
因为您基本上覆盖了 JFrame
的基本逻辑,因此您可能(并且很可能会)破坏某些东西。
2。第二件事是 将组件放入具有约束的网格单元不会自动将它们拉伸到整个区域。如果引用documentation,你会看到:
Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.
3。自动拉伸可以通过fill
属性的约束来实现。但是您仍然需要以某种方式声明所需的尺寸。
4.可以使用约束的 weightx
和 weighty
属性设置 sizes 规范。
Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.
因此,在您的简单情况下,您基本上有两列,其中一列的权重为 0.7
,另一列的权重为 0.3
。
下面的代码演示了完整的解决方案:
class MyFrame extends JFrame {
private JPanel contentPane;
public MyFrame (){
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.2/0.3;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
contentPane.add(blue, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.1/0.3;
gbc.weighty = 1;
JPanel green = new JPanel();
green.setBackground(Color.green);
contentPane.add(green, gbc);
setContentPane(contentPane);
setPreferredSize(new Dimension(630, 650));
pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
我目前正在使用 GUI 迈出第一步,现在我只是想根据我的艺术作品制作一个 JFrame
和两个 JPanel
容器:
我选择 GridBagLayout
因为我希望它以后可以调整大小但保持给定的比例,但我很难理解所有这些是如何工作的。
我当前的代码如下所示:
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
private JPanel contentPane;
public JPanel getContentPane() {
return contentPane;
}
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
public MyFrame (){
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 3;
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
contentPane.add(blue, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 3;
JPanel gruen = new JPanel();
gruen.setBackground(Color.green);
contentPane.add(gruen, gbc);
this.setPreferredSize(new Dimension(630, 650));
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setContentPane(contentPane);
}
public static void main(String[] args) {
new MyFrame();
}
}
这给了我这个空 window:
如果有人能告诉我我做错了什么,那就太好了。
1.首先,你需要删除这个:
public JPanel getContentPane() {
return contentPane;
}
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
因为您基本上覆盖了 JFrame
的基本逻辑,因此您可能(并且很可能会)破坏某些东西。
2。第二件事是 将组件放入具有约束的网格单元不会自动将它们拉伸到整个区域。如果引用documentation,你会看到:
Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.
3。自动拉伸可以通过fill
属性的约束来实现。但是您仍然需要以某种方式声明所需的尺寸。
4.可以使用约束的 weightx
和 weighty
属性设置 sizes 规范。
Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.
因此,在您的简单情况下,您基本上有两列,其中一列的权重为 0.7
,另一列的权重为 0.3
。
下面的代码演示了完整的解决方案:
class MyFrame extends JFrame {
private JPanel contentPane;
public MyFrame (){
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.2/0.3;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
contentPane.add(blue, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.1/0.3;
gbc.weighty = 1;
JPanel green = new JPanel();
green.setBackground(Color.green);
contentPane.add(green, gbc);
setContentPane(contentPane);
setPreferredSize(new Dimension(630, 650));
pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}