尝试使用GridBaglayout来管理Java中的Jpanels
我目前正在使用Guis迈出第一步,现在正试图根据我的艺术品在此处使用两个Jprame
使用两个Jpanel
容器:
作品。
我当前的代码看起来像这样:
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();
}
}
给我这个空窗口:
I am currently making my first steps with GUIs and am right now just trying to make a JFrame
with two JPanel
containers according to my work of art here:
I chose the GridBagLayout
since I want it to be resizeable later but keep the given proportions, yet I'm having a hard time understanding how all of this works.
My current code looks like this:
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();
}
}
Which gives me this empty window:
If someone could tell me what I'm doing wrong that would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1。首先,您需要删除以下内容:
,因为您基本上覆盖了
jframe
的基本逻辑,因此您可以(并且可能)破坏了某些内容。2。第二件事是将组件放在网格单元上,并不会自动在整个区域延伸。如果要引用 documentation ,您会看到:
3。可以使用
填充
约束的属性来实现自动拉伸。但是您仍然需要以某种方式声明所需的尺寸。4。尺寸规格可以使用
stogex
和weefty
约束的属性。因此,在您的简单情况下,您基本上有两列,其中一个具有
0.7
的X重量,另一个具有0.3
。以下代码演示了完整的解决方案:
1. First of all, you need to remove this:
because you basically override the base logic of
JFrame
, hence you may (and likely will) break something.2. The second thing is that putting component to the grid cells with constraints does not automatically stretch them across the area. If to refer to the documentation, you will see that:
3. Automatic stretching can be achieved with
fill
property of constraints. But you still need to declare the required sizes somehow.4. The sizes specification can be set up with
weightx
andweighty
properties of constraints.So in your simple case you basically have two columns where one have
0.7
of x weight and another have0.3
.The below code demonstrates the complete solution: