将JLabel置于JPanel中心 [英] Centering a JLabel in a JPanel

查看:175
本文介绍了将JLabel置于JPanel中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自 JPanel 的面板。我有一个自定义控件派生自 JLabel 。我试图在我的面板中居中这个自定义 JLabel

I have a panel derived from JPanel. I have a custom control derived from JLabel. I am attempting to center this custom JLabel on my panel.

我知道这样做的唯一方法就是工作是使用null布局( setLayout(null)),然后计算自定义JLabel的 setLocation()点所以它在正确的位置。

The only way I know to do this that will work is to use the a null layout (setLayout(null)) and then calculate the custom JLabel's setLocation() point so that it's in the right spot.

自定义 JLabel 在这个实际上从一个面板移动到此面板应用程序,我相信之前在 setLocation 中设置的位置正在影响一些事情。但是,当我将其设置为(0,0)时,组件会进入左上角。

The custom JLabel is physically moved from one panel to this panel in this app and I believe the location previously set in setLocation is affecting things. However when I set it to (0,0) the component goes up into the upper left corner.

BorderLayout 不起作用,因为只提供了1个组件并放入 BorderLayout .CENTER ,中央部分扩展以填充所有空间。

BorderLayout doesn't work because when only 1 component is provided and placed into BorderLayout.CENTER, the central section expands to fill all of the space.

我从其他网站剪切并粘贴的示例 BoxLayout component.setAlignmentX(Component.CENTER_ALIGNMENT)。这也不起作用。

An example I cut and pasted from another site used BoxLayout and component.setAlignmentX(Component.CENTER_ALIGNMENT). This didn't work either.

提到的另一个答案覆盖了面板的 getInset()函数(我认为这是什么它被称为),但事实证明这是一个死胡同。

Another answer mentioned overriding the panel's getInset() function (I think that's what it was called), but that proved to be a dead end.

到目前为止,我正在使用一个带有 GridBagLayout 布局,当我将自定义 JLabel 插入我的面板时,我包含一个 GridBagConstraints 对象。但这是低效的。有没有更好的方法将 JLabel 集中在我的 JPanel

So far I'm working with a panel with a GridBagLayout layout and I include a GridBagConstraints object when I insert the custom JLabel into my panel. This is inefficient, though. Is there a better way to center the JLabel in my JPanel?

推荐答案

为<设置 GridBagLayout a href =http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html> JPanel ,输入JLabel 没有任何 GridBagConstraints JPanel JLabel 将集中在一起

Set GridBagLayout for JPanel, put JLabel without any GridBagConstraints to the JPanel, JLabel will be centered

示例

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

public class CenteredJLabel {

    private JFrame frame = new JFrame("Test");
    private JPanel panel = new JPanel();
    private JLabel label = new JLabel("CenteredJLabel");

    public CenteredJLabel() {
        panel.setLayout(new GridBagLayout());
        panel.add(label);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                CenteredJLabel centeredJLabel = new CenteredJLabel();
            }
        });
    }
}

这篇关于将JLabel置于JPanel中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆