【发布时间】:2015-06-01 09:15:46
【问题描述】:
我已将 JLabel 的宽度设置为字符串的宽度,但是当我这样做时,JLabel 会使用 ... 切断中间的字符串,因此不是“问我一个问题!”,它最终成为“问我一个问题......”。这可能是我使用的字体的问题,但我不确定。有什么帮助吗?
这是我的自定义 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);
}
}
【问题讨论】:
-
为什么要使用 setLocation 和 setSize 却不让布局管理器去做呢?
-
@MadProgrammer 因为 LayoutManager 可能不会做我想做的事。我希望它们在特定的地方,而 LayoutManager 可能不会把它们放在那里。
-
在设置文本和字体后尝试使用 getPreferredSize,但您可能会遇到可变宽度字体的问题
-
@MadProgrammer 但是我必须设置一个特定的大小......
-
为什么?你有什么保证任何字体是固定或可变宽度的,Java没有它自己的字体格式,它只是使用系统字体(TFT或其他)。 getPreferredSize 是一个可用于从 Component 扩展的任何组件的方法,因此它也可用于任何 Swing 组件...
标签: java fonts jpanel width jlabel