如何在 JLabel 中右对齐文本?
我有以下代码:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int xx =0; xx < 3; xx++)
{
JLabel label = new JLabel("String");
label.setPreferredSize(new Dimension(300,15));
label.setHorizontalAlignment(JLabel.RIGHT);
panel.add(label);
}
这就是我希望文本的外观:
[ String]
[ String]
[ String]
这就是它的外观
[String]
[String]
[String]
由于某种原因,标签没有设置为我指定的首选大小,我认为因此它没有正确对齐我的标签文本。但我不确定。任何帮助将不胜感激。
I have the following code:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int xx =0; xx < 3; xx++)
{
JLabel label = new JLabel("String");
label.setPreferredSize(new Dimension(300,15));
label.setHorizontalAlignment(JLabel.RIGHT);
panel.add(label);
}
This is how I would like the text to look:
[ String]
[ String]
[ String]
this is how it looks
[String]
[String]
[String]
For some reason label doesn't get set to the preferred size I specified and I think because of this it doesn't right align my label text. But im not sure. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
:)
:)
setPreferredSize/MinimumSize/MaximumSize 方法依赖于父组件(在本例中为面板)的布局管理器。
首先尝试使用 setMaximumSize 而不是 setPreferredSize,如果我没有出错的话应该使用 BoxLayout。
另外:可能你必须使用和使用胶水:
如果你需要 Y_AXIS BoxLayout,你也可以使用嵌套面板:
The setPreferredSize/MinimumSize/MaximumSize methods is dependent from the layout manager of the parent component (in this case panel).
First try with setMaximumSize instead of setPreferredSize, if I'm not going wrong should work with BoxLayout.
In addition: probably you have to use and play around with glues:
If you need the Y_AXIS BoxLayout you could also used nested panel:
我认为这取决于您正在使用的布局,在 XY 中(我记得是 JBuilder 中的某种布局)它应该可以工作,但在其他情况下可能会出现问题。尝试将最小尺寸更改为首选尺寸。
I think it depend to layout that you are using, in XY (I remember was some kind of layouts in JBuilder) it should work, but in other can be problem. Try to change minimum size to prefered size.
这有点烦人,但如果您希望比网格布局更灵活地对齐,您可以使用带有框布局的嵌套 JPanel。
我使用水平胶水将其保持在正确的位置,无论大小如何,但您可以放置刚性区域以使其具有特定的距离。
This is a bit annoying, but you could use nested JPanels with box layouts if you wanted more flexibility in your alignment than with grid layout.
I used horizontal glue to keep it at the right no matter the size, but you can put in rigid areas to make it a specific distance.
您需要确保您的
LayoutManager
正在调整标签大小以填充目标区域。您可能有一个JLabel
组件,其大小与文本长度完全一致,并且在布局中左对齐。You need to ensure that your
LayoutManager
is sizing the label to fill the target area. You probably have aJLabel
component which is exactly sized to the length of the text and which has been left aligned in the layout.而不是使用
use
因此你有:
rather than use
use
thus you have:
不能用下面的吗?
这至少可以在
JFrame
容器中运行。不确定JPanel
。Couldn't you use the following?
This works within a
JFrame
Container at least. Not sure about aJPanel
.根据你们的回复,我确定 BoxLayout 不支持我想要的文本对齐方式,因此我将其更改为
,一切正常。
With the responses from you guys I was able to determine that BoxLayout doesn't support the text alignment that I wanted, so I changed it to
and everything worked fine.