java JLabel宽度不工作
我已经将JLable的宽度设置为字符串的宽度,但是当我这样做时,JLabp使用……因此,与其说“问我一个问题!”,不如说,结果是“问我一个问题……”。这可能是我使用的字体有问题,但我不确定。有什么帮助吗
下面是我的自定义JLabel类BLANKJLabel:
package BLANK.BLANK.menufeatures;
import BLANK.BLANK.main.QnABotJPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
public class BLANKJLabel extends JLabel {
public AffineTransform affineTransform = new AffineTransform();
public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);
public BLANKJLabel(ImageIcon icon, String text, int x, int y) {
int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());
this.setText(text);
this.setFont(BLANKJPanel.font);
this.setLocation(x, y);
this.setIcon(icon);
this.setSize(stringWidth, stringHeight);
}
public BLANKLabel(String text, int x, int y) {
int stringWidth = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getWidth());
int stringHeight = (int) (QnABotJPanel.font.getStringBounds(text, fontRenderContext).getHeight());
Dimension d = new Dimension(this.getPreferredSize());
this.setText(text);
this.setFont(BLANKJLabel.font);
this.setLocation(x, y);
this.setSize(d);
}
}
下面是使用它的类BLANKJPanel:
package BLANK.BLANK.main;
import BLANK.BLANK.menufeatures.BLANKButton;
import BLANK.BLANK.menufeatures.BLANKJLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
public class QnABotJPanel extends JPanel {
public AffineTransform affineTransform = new AffineTransform();
public FontRenderContext fontRenderContext = new FontRenderContext(affineTransform, true, true);
static QnABotButton submitButton;
static QnABotJLabel askMeAQuestionLabel;
public static Font font = new Font("Avenir Next", Font.PLAIN, 20);
String askMeAQuestion = "Ask me a question!";
int askMeAQuestionWidth = (int)(font.getStringBounds(askMeAQuestion, fontRenderContext).getWidth());
int askMeAQuestionHeight = (int)(font.getStringBounds(askMeAQuestion, fontRenderContext).getHeight());
public QnABotJPanel() {
setLayout(new GroupLayout(this));
submitButton = new QnABotButton("Submit", "/submit", QnABotWindow.windowWidth / 2 - 100, (QnABotWindow.windowHeight / 4) * 3 - 50);
askMeAQuestionLabel = new QnABotJLabel(askMeAQuestion, QnABotWindow.windowWidth / 4 - askMeAQuestionWidth / 2, QnABotWindow.windowHeight / 3 - askMeAQuestionHeight / 2);
this.add(submitButton);
this.add(askMeAQuestionLabel);
this.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (QnABotJPanel.submitButton.getActionCommand().equalsIgnoreCase(e.getActionCommand())) {
}
}
});
}
public void addActionListener(ActionListener listener) {
submitButton.addActionListener(listener);
}
}
# 1 楼答案
通过添加常量来增加stringWidth的大小
# 2 楼答案
下面是一种可以操作JLabel的方法(在本例中,它应用于JPOppMenu)
# 3 楼答案
我给你的第一条建议是避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。有太多的因素会影响零部件的单个尺寸,而这些因素都是您无法控制的。Swing的设计初衷是与布局经理一起工作,抛弃它们将导致问题层出不穷,你将花费越来越多的时间来纠正这些问题您可能遇到的下一个问题是尝试执行标签已经能够执行的操作,计算其首选大小
如果在设置文本和字体后使用
JLabel#getPreferredSize
,它将告诉您组件的大小。默认情况下,布局管理器API就是这样做的以及利用布局管理API