Java将JLabel对齐在JPanel的中心 [英] Java align JLabel in center of JPanel

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

问题描述

我的应用程序顶部有一个栏,在JLabel的任一侧有许多按钮。按钮的可见性取决于用户正在执行的当前任务,其中一个按钮的文本也可能会根据当前状态而改变。

I have a bar at the top of my application that has a number of buttons either side of a JLabel. The button's visibility is dependent upon the current task a user is carrying out and one of the button's text may also change depending on the current state.

我想做什么有一些按钮贴在JPanel的左边,JLabel的中心位于JPanel的正中央,其余的按钮位于JPanel的右边。

What I would like to do is have a number of buttons stick to the left of the JPanel, the JLabel's center to be in the very center of the JPanel and the rest of the buttons to be to the right of the JPanel.

到目前为止,我已经设法使用各种方法和布局管理器让按钮左右两侧,但我不能让JLabel的中心位于JPanel的死点。我设法获得的最好的标签是靠近中心的标签,但随着按钮设置为可见或文本发生变化,标签会移动。

So far I have managed to get the buttons sticking to the left and the right using various methods and layout managers but I cannot get the JLabel's center to be in the dead center of the JPanel. The best I have managed to get is the label near enough to the center but it would then move around as the buttons are set to visible or their text changes.

是吗是否可能迫使JLabel保持死点?

Is it possible to force the JLabel to remain dead center?

注意:按钮永远不会变得足够大以满足JLabel的任何边缘,因此这不是问题。 / p>

Note: the buttons will never become big enough to meet either edge of the JLabel, so that is not a problem.

推荐答案

正如我所看到的那样,您需要以首选尺寸显示标签,然后您需要左右面板同等填充窗口中可用的剩余空间。

As I see it you need the label to be displayed at its preferred size and then you need a left and right panel to equally fill the remaining space available in the window.

您可以使用相对布局类。

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

public class RelativeSSCCE extends JPanel
{
    public RelativeSSCCE()
    {
        JPanel left = new JPanel( new FlowLayout(FlowLayout.LEFT) );
        left.add( new JButton("L1") );
        left.add( new JButton("L2") );
        left.add( new JButton("L3") );

        JLabel center = new JLabel("Centered");

        JPanel right = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
        right.add( new JButton("Right1") );
        right.add( new JButton("Right2") );
        right.add( new JButton("Right3") );

        // choose your layout manager here

        setLayout( new RelativeLayout() );
        Float ratio = new Float(1);
        add(left, ratio);
        add(center);
        add(right, ratio);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Basic RelativeSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new RelativeSSCCE() );
        frame.setSize(600, 100);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

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

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

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