BorderLayout.CENTER 上 GridBagLayout 面板的垂直对齐

发布于 2024-10-01 17:46:31 字数 1183 浏览 5 评论 0原文

我想做的是将 GridBagLayout 面板放在 BorderLayout 的中心,并将 GridBagLayout 面板(/和上面的文本)垂直对齐到顶部(因为它会自动将其水平和垂直地放在中间)。

所以我基本上尝试过(但最终 GridBagLayout 的文本仍然位于页面的中间,而不是中间的 x 和顶部 y):

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.imageio.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.awt.event.*;

public class Test extends JApplet implements MouseListener, ActionListener {

 public void init() {
    //create borderlayout
    this.setLayout(new BorderLayout());
    //create a GridBagLayout panel
    JPanel gb = new JPanel(new GridBagLayout());
    JLabel content = new JLabel("Some text");
    //set GridBagConstraints (gridx,gridy,fill,anchor)
    setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);
    gb.add(content, gbc); //gbc is containing the GridBagConstraints
    this.add(gb, BorderLayout.CENTER);
  }

}

所以我尝试使用 gridbagconstraints 锚点将对齐方式设置为垂直向北,顶部,但这似乎不起作用。我还尝试调整 GridBagLayout 面板本身的大小(使用 panel.setSize 和 setPreferredSize 使其具有布局的完整高度,100%),然后使用 gridbagconstraints.anchor 垂直对齐其上的元素,但这不起作用任何一个。

谁能帮我解决这个问题吗?

预先致谢,

最诚挚的问候, 斯凯夫。

所以我的问题是

What I'm trying to do is place a GridBagLayout Panel on the center of my BorderLayout and vertical align the GridBagLayout panel ( /and text on it ) to the TOP ( because it automaticly puts it in the middle, horizontally AND vertically ).

So what I basicly tried ( but ended up having the text of the GridBagLayout still in the total middle of the page instead of in the middle x and top y):

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.imageio.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.awt.event.*;

public class Test extends JApplet implements MouseListener, ActionListener {

 public void init() {
    //create borderlayout
    this.setLayout(new BorderLayout());
    //create a GridBagLayout panel
    JPanel gb = new JPanel(new GridBagLayout());
    JLabel content = new JLabel("Some text");
    //set GridBagConstraints (gridx,gridy,fill,anchor)
    setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);
    gb.add(content, gbc); //gbc is containing the GridBagConstraints
    this.add(gb, BorderLayout.CENTER);
  }

}

So I tried to use the gridbagconstraints anchor to set the alignment vertically to the north, top, but that seems not to work. I also tried to resize the GridBagLayout panel itself ( to make it have full height of the layout, 100%, using panel.setSize and setPreferredSize ) and then vertically align the elements on it using the gridbagconstraints.anchor, but that didn't work either.

Can anyone help me out on this?

Thanks in advance,

Best Regards,
Skyfe.

So my question is

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小矜持 2024-10-08 17:46:31

使用 GridBagConstraints 类的每个属性之前,请仔细查看其 Javadoc。

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {

    public static void main(String arg[]) {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JPanel gb = new JPanel(new GridBagLayout());
        JLabel content = new JLabel("Some text");

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weighty = 1;

        gb.add(content, gbc); // gbc is containing the GridBagConstraints
        frame.add(gb, BorderLayout.CENTER);

        frame.setVisible(true);
    }

}

Take a careful look at the Javadoc of each property of the GridBagConstraints class before using it.

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {

    public static void main(String arg[]) {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JPanel gb = new JPanel(new GridBagLayout());
        JLabel content = new JLabel("Some text");

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weighty = 1;

        gb.add(content, gbc); // gbc is containing the GridBagConstraints
        frame.add(gb, BorderLayout.CENTER);

        frame.setVisible(true);
    }

}
煞人兵器 2024-10-08 17:46:31

除了将fill 设置为NONE 之外,还将weighty 设置为非0 值(例如1.0)。这告诉布局将 GridBag 列中的所有额外空间分配给该特定单元格,但由于标签本身锚定在北方并设置为不填充,因此额外空间将分配在标签下方。

In addition to setting fill to NONE, also set weighty to a non-0 value (e.g. 1.0). This tells the layout to allocate all extra space in the GridBag column to that particular cell, but since the label itself is anchored to the north and set to not fill, the extra space will be allocated beneath the label.

纵性 2024-10-08 17:46:31

setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);

正在使组件(“内容”)垂直填充 GB 。这可能就是为什么您认为您的组件(内容)仍在中间。尝试将其设置为 GridBagConstraints.NONE 。
这会将您的内容与具有 GridBagLayout 的面板内的顶部对齐。

对于外部组件,您始终可以使用类似以下内容:

panel.add(button, BorderLayout.PAGE_START);

并查看它是否有效。

Your

setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);

is making the component ("content") filling gb vertically. This may be why you think your component(content) is still in the middle. Try setting it to GridBagConstraints.NONE.
This will align your content to the top inside the panel that has GridBagLayout.

for the outer component, you can always use something like:

panel.add(button, BorderLayout.PAGE_START);

and see if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文