gpt4 book ai didi

java - 如何真正将 JFrame 中的对象居中?

转载 作者:行者123 更新时间:2023-12-02 01:29:39 25 4
gpt4 key购买 nike

我有一个扩展 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 的 native 移动开头造成的吗?我可以计算吗?

最佳答案

How to indeed center object in a JFrame?

确实将其添加为使用 GridBagLayout 添加到 JPanel 的唯一组件。使用该面板作为内容 Pane 。

示例:

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);
}
}

关于java - 如何真正将 JFrame 中的对象居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com