【问题标题】:Center JLabel in BorderLayout with a button at line end在 BorderLayout 中将 JLabel 居中,在行尾有一个按钮
【发布时间】:2015-06-11 09:45:52
【问题描述】:

我的挥杆布局的一部分有一个标题,我想让文本在该部分的整个宽度上水平居中,但也只有右侧有一个按钮。它应该是这样的:

 /------Same width------\           /------Same width------\
[------------------------]Text here[----------------[Button]]

我目前使用的是BorderLayout,文字居中,按钮在行尾,但文字居中不计按钮,如下:

 /----Same width----\           /---Same width----\
[--------------------]Text here[-------------------][Button]]

【问题讨论】:

    标签: java swing layout-manager border-layout


    【解决方案1】:

    这是一种使用 OverlayLayout 的方法,该方法旨在在 z 轴上绘制多个组件:

    import java.awt.*;
    import javax.swing.*;
    
    public class SSCCE extends JPanel
    {
        public SSCCE()
        {
            JLabel label = new JLabel("I'm a Centered Label");
            Box labelBox= Box.createHorizontalBox();
            labelBox.add(Box.createHorizontalGlue());
            labelBox.add(label);
            labelBox.add(Box.createHorizontalGlue());
    
            JButton button = new JButton("Button");
            Box buttonBox= Box.createHorizontalBox();
            buttonBox.add(Box.createHorizontalGlue());
            buttonBox.add(button);
    
            setLayout( new OverlayLayout(this) );
            add(buttonBox);
            add(labelBox);
        }
    
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("SSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new SSCCE(), BorderLayout.NORTH);
            frame.setLocationByPlatform( true );
            frame.pack();
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
        }
    }
    

    随着宽度的减小,按钮将在标签上绘制。

    【讨论】:

    • 很好地使用OverlayLayoutBox胶水+1
    【解决方案2】:

    我不确定这是否是您真正想要的答案,但您可以使用不同的布局管理器,例如 GridBagLayout

    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class JavaApplication295 {
    
        public static void main(String[] args) {
            new JavaApplication295();
        }
    
        public JavaApplication295() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
    //          gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.weightx = 1;
    
                add(new JLabel("Look ma, no hands"), gbc);
    
                gbc.anchor = GridBagConstraints.EAST;
                add(new JButton("No Allowance"), gbc);
            }
    
        }
    
    
    }
    

    现在,问题在于,这两个组件实际上定位在同一个位置,不同的是,按钮锚定在单元格的右侧位置,这意味着在计算布局时,它们会重叠....

    【讨论】:

    • 1+,棘手,我不知道多个组件可以共享同一个单元格:)
    • @camickr 是的,你错误地发现的东西:P
    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2013-04-06
    • 2014-09-06
    • 1970-01-01
    • 2013-11-03
    • 2012-04-07
    • 2012-02-04
    相关资源
    最近更新 更多