在BorderLayout中使JLabel居中,行尾有一个按钮 [英] Center JLabel in BorderLayout with a button at line end

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

问题描述

我的挥杆布局的一部分上有一个标题,我想使文本在该部分的整个宽度上水平居中,但也要在右侧有一个按钮.它应该看起来像这样:

I have a header on a section of my swing layout, and I want to have text centered horizontally across the whole width of the section, but also have a button on only the right side. It should look like this:

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

我当前正在使用BorderLayout,其文本居中,而按钮位于行尾,但是文本居中,不包括按钮,例如:

I am currently using a BorderLayout, with the text in the center and the button at the line end, but the text is centered not counting the button, as such:

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

推荐答案

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

I'm not sure if this is the answer you really want, but you could use a different layout manager, like 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);
        }

    }


}

现在,问题在于,两个组件实际上都位于同一位置,不同之处在于,按钮被锚定在单元格的正确位置,这意味着在计算布局时,它们将重叠....

Now, the problem with this, is both components are actually positioned at the same location, the difference is, the button is anchored to the right position of the cell, this means that when the layout is been calculated, they will overlap....

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

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