如何确实在JFrame中居中放置对象?

时间:2019-05-26 12:25:09

标签: java swing jframe window layout-manager

我有一个扩展Jframe的类,并使用setLocationRelativeTo(null);将其居中。 layouManager也为null

我的JFrame:

mport com.sxf.protocol.chat.util.window.AbstractField;

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

public class Field extends AbstractField {

    private Container cp;

    public Field() {
        super("Test");

        buildWindow();
    }

    private void buildWindow() {

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setPreferredSize(new Dimension(frameWidth, frameHeight));
        setResizable(false);

        cp = getContentPane();
        cp.setLayout(null);

        initComponents();

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void initComponents() {
        JLabel label = new JLabel("Test", SwingConstants.CENTER);
        JLabel border = new JLabel("");
        label.setSize(200, 30);
        label.setLocation((frameWidth - label.getWidth()) / 2, (frameHeight - label.getHeight()) / 2);
        border.setBounds(frameWidth / 2, 0, frameWidth / 2, frameHeight);
        label.setBorder(new LineBorder(new Color(0, 0, 0)));
        border.setBorder(new LineBorder(new Color(255, 0, 0)));

        cp.add(label);
        cp.add(border);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Field();
            }
        });
    }
}

我添加了边框以查看其中心是否正确。

AbstractField(其中只有一些计算):

public abstract class AbstractField extends JFrame {

    private final static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private static final Dimension frameSize = new Dimension(screenSize.width / 4, screenSize.height / 4);
    protected static final int frameWidth;
    protected static final int frameHeight;
    protected static final int x;
    protected static final int y;

    static {
        frameWidth = frameSize.width;
        frameHeight = frameSize.height;
        x = (screenSize.width - frameWidth) / 2;
        y = (screenSize.height - frameHeight) / 2;
    }

    public AbstractField(String title) {
        super(title);
    }
}

但是当我尝试将组件居中时,例如JLabel在做
(frame.getWidth - component.getWidth) / 2,并且高度相同,实际上并没有居中,而是稍微偏右。

那是由于jframe的原生移动而引起的,我可以计算出来吗?

1 个答案:

答案 0 :(得分:2)

  

如何在JFrame中居中放置对象?

实际上将其添加为唯一通过JPanel添加到GridBagLayout的组件。使用该面板作为内容窗格。

示例:

enter image description here

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

public class CenteredLabel {

    private JComponent ui = null;

    CenteredLabel() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JLabel centeredLabel = new JLabel("Test");
        centeredLabel.setBorder(new LineBorder(Color.RED));
        ui.add(centeredLabel);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            CenteredLabel o = new CenteredLabel();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}