简体   繁体   English

Java将JLabel与JPanel的中心对齐

[英]Java align JLabel in center of JPanel

I have a bar at the top of my application that has a number of buttons either side of a JLabel. 我的应用程序顶部有一个栏,在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. 按钮的可见性取决于用户正在执行的当前任务,并且按钮的文本之一也可能根据当前状态而改变。

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. 我想要做的是在JPanel的左边有一些按钮,JLabel的中心位于JPanel的正中央,其余的按钮位于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. 到目前为止,我已经设法使用各种方法和布局管理器使按钮左侧和右侧,但我不能让JLabel的中心位于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. 我设法获得的最好的标签是靠近中心的标签,但随着按钮设置为可见或文本发生变化,它会移动。

Is it possible to force the JLabel to remain dead center? 是否有可能迫使JLabel保持死亡中心?

Note: the buttons will never become big enough to meet either edge of the JLabel, so that is not a problem. 注意:按钮永远不会变得足够大以满足JLabel的任何一个边缘,因此这不是问题。

You could also use a BoxLayout , with X_AXIS layout, and add to the container 您还可以使用带有X_AXIS布局的BoxLayout ,并添加到容器中

Edit: Here's an example 编辑:这是一个例子

panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(innerPanel);
panel.add(Box.createHorizontalGlue());

Use BorderLayout . 使用BorderLayout Add the JLabel with the BorderLayout.CENTER constraint and add a pair of JPanels with the BorderLayout.EAST and BorderLayout.WEST constraints. 使用BorderLayout.CENTER约束添加JLabel,并添加一对带有BorderLayout.EAST和BorderLayout.WEST约束的JPanel。 Add your additional buttons to the appropriate JPanel. 将其他按钮添加到相应的JPanel。

To have the text appear centered, you also need to set the Label's horizontalAlignment to JLabel.CENTER. 要使文本显示为居中,您还需要将Label的horizo​​ntalAlignment设置为JLabel.CENTER。

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. 在我看来你需要标签以其首选尺寸显示,然后你需要一个左右面板来同样填充窗口中可用的剩余空间。

You can use the Relative Layout class. 您可以使用Relative Layout类。

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();
            }
        });
    }
}
setAlignmentX(java.awt.Component.CENTER_ALIGNMENT)

and

setAlignmentY(java.awt.Component.CENTER_ALIGNMENT) 

may help if used with the elements which have to be centered, eg JPanel 如果与必须居中的元素一起使用可能会有所帮助,例如JPanel

可能是我的necroposting,但setAlignmentX(java.awt.Component.CENTER_ALIGNMENT)和setAlignmentY(java.awt.Component.CENTER_ALIGNMENT)可能会有所帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM