gpt4 book ai didi

java - JLabel 的变量不会居中?

转载 作者:行者123 更新时间:2023-12-01 18:12:19 24 4
gpt4 key购买 nike

我目前正在使用绝对布局,我想将一个字符串变量放置到面板上(此框架类似于弹出对话框)并水平对齐。这是我的代码片段。

JLabel label = new JLabel(loggedInAs);
label.setBounds(10, 61, 314, 23);
label.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(label);

当我运行此命令时,文本似乎稍微向右开始,但是如果我在标签中输入预设文本,例如

JLabel label = new JLabel("Hello");

它将居中。有什么办法可以解决这个问题吗?

(我也玩过 miglayout,但从我的主 JFrame 运行它时结果完全不同)

一个例子:

enter image description here

Here is an example of what I mean by it being slightly to the right

最佳答案

我认为您的问题在于使用绝对布局,因为您使用硬编码数字来放置 JLabel。标签文本本身居中良好,但如果您在标签周围放置边框,您可能会看到标签本身(而不是其文本)向右倾斜。最简单的解决方案是让布局管理器为您完成这些工作。例如,下面的代码创建一个成功登录的对话框,该对话框使用布局管理器的组合来实现您在图像中尝试执行的操作:

enter image description here

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class LoginPanelEg extends JPanel {
private JTextField nameField = new JTextField(20);
private Action LoginAction = new LoginAction("Login");

public LoginPanelEg() {
nameField.setAction(LoginAction);
add(new JLabel("Login Name:"));
add(nameField);
add(new JButton(LoginAction));
}

private class LoginAction extends AbstractAction {
public LoginAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic); // alt-key comb
}

@Override
public void actionPerformed(ActionEvent e) {
LoginPanel loginPanel = new LoginPanel(nameField.getText());

Window win = SwingUtilities.getWindowAncestor(LoginPanelEg.this);
JDialog dialog = new JDialog(win, "Successfully Logged In", ModalityType.APPLICATION_MODAL);
dialog.add(loginPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("LoginPanelEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LoginPanelEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

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

@SuppressWarnings("serial")
class LoginPanel extends JPanel {
private static final int PREF_W = 350;
private static final int PREF_H = 200;
private static final String SUCCESSFULLY_LOGGED = "Successfully logged in as:";
private static final int GAP = 20;
private static final Font PROMPT_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 14);
private static final Font NAME_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 12);
private JLabel successfullyLoggedLabel = new JLabel(SUCCESSFULLY_LOGGED, SwingConstants.CENTER);
private JLabel nameLabel = new JLabel("", SwingConstants.CENTER);

public LoginPanel() {
successfullyLoggedLabel.setFont(PROMPT_FONT);
nameLabel.setFont(NAME_FONT);

JPanel innerPanel = new JPanel(new GridBagLayout());
innerPanel.add(successfullyLoggedLabel, createGbc(0, 0));
innerPanel.add(nameLabel, createGbc(0, 1));

JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new CloseAction("Close")));

setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout());
add(innerPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}

LoginPanel(String name) {
this();
nameLabel.setText(name);
}

public void setName(String name) {
nameLabel.setText(name);
}

private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
return gbc;
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private class CloseAction extends AbstractAction {
public CloseAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic); // alt-key comb
}

@Override
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
if (component == null) {
return;
}
Window win = SwingUtilities.getWindowAncestor(component);
if (win == null) {
return;
}
win.dispose();
}
}
}

关于java - JLabel 的变量不会居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093064/

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