在 JPanel 中居中 JLabel [英] Centering a JLabel in a JPanel

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

问题描述

我有一个从 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.

我知道的唯一可行的方法是使用空布局 (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.

我从另一个站点剪切和粘贴的示例使用了 BoxLayoutcomponent.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 对于 JPanel,输入 JLabel 没有任何 GridBagConstraintsJPanelJLabel 将居中

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();
            }
        });
    }
}

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

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