如何在 NetBeans 中控制 JButton HTML 文本的居中?
我正在尝试在 JButton 上放置两行文本;例如
+----------+
| READER |
| STOP |
+----------+
,但我无法将其集中在按钮上。我转到 JButton 的属性编辑器,然后为 text 属性输入
READER
STOP
。这会导致两个单词相对于彼此居中,但它们一起看起来仍然向按钮表面的右侧移动,如:+----------+
| READER|
| STOP |
+----------+
还有水平和水平。垂直对齐和文本位置属性,我已将其全部设置为 CENTER,但这没有任何我可以看到的效果。
编辑:这是当我完全省略
时它的布局方式的说明,以防有人对我的描述“READER 和 STOP 是左对齐的”感到困惑。其他,但在按钮上右对齐”:+----------+
| READER|
| STOP |
+----------+
编辑:这是 NetBeans 生成的代码:
readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive. ");
readerStopButton_.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
readerStopButton_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
readerStopButton_ActionPerformed(evt);
}
});
operationButtons_.add(readerStopButton_);
编辑:这是按钮在我看来的屏幕截图。关于布局我有很多不了解的地方,所以我很可能遗漏了一些重要的信息。但基本上我让 NetBeans 完成所有工作,除了提供 HTML 文本。
编辑:放置显示所有按钮的替换屏幕截图。请注意,不使用 HTML 的(单个单词的)对齐正确,而使用 HTML 的两个则混乱。
I'm trying to put a two-line piece of text on a JButton; e.g.
+----------+
| READER |
| STOP |
+----------+
But I'm having trouble getting it centered on the button. I go to the Property editor for the JButton, and enter <html><center>READER<br>STOP
for the text property. This causes the two words to be centered with respect to each other, but together they still appear to be shifted toward the right side of the face of the button, as in:
+----------+
| READER|
| STOP |
+----------+
There are also Horizontal & Vertical Alignment & Text Position properties, which I've set all to CENTER, but this doesn't have any effect that I can see.
EDIT: here is an illustration of how it is laid out when I omit <center>
entirely, in case anyone is confused by my description that "READER and STOP are left-justified w/respect to each other, but right-justified on the button":
+----------+
| READER|
| STOP |
+----------+
EDIT: here is the code generated by NetBeans:
readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive. ");
readerStopButton_.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
readerStopButton_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
readerStopButton_ActionPerformed(evt);
}
});
operationButtons_.add(readerStopButton_);
EDIT: Here's a screen capture of how the button looks to me. There is a lot I don't know about doing layouts, so it's quite possible I'm omitting some crucial info. But basically I'm letting NetBeans do all the work, except for providing the HTML text.
EDIT: Putting up a replacement screen shot that shows all buttons. Note that the ones that don't use HTML (the single-word ones) are correctly aligned, while the two that do use HTML are messed up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要忘记结束标记:
另请参阅如何使用Swing 组件中的 HTML。
附录:我无耻地克隆了@mre的更正上面的 HTML 以生成以下图像:
Don't forget the closing tag:
See also How to Use HTML in Swing Components.
Addendum: I have shamelessly cloned @mre's correct HTML above to produce the following image:
我不确定你在说什么。我使用了您包含的代码,它工作得很好。
HTML 文本居中对齐。很难想象你会看到不同的东西。
I'm not sure what you're talking about. I used the code you've included and it works just fine.
The HTML text is center justified. It's hard to imagine that you're seeing something different.
原因是您限制了按钮的大小,默认情况下按钮有一个边距,您可以按如下方式删除边距:
the reason is you limited the size of the button, by default there is a margin for button, you can remove the margin as follows: