java 旋转 Swing JLabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/620929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotate a Swing JLabel
提问by Joey
I am currently trying to implement a Swing component, inheriting from JLabelwhich should simply represent a label that can be oriented vertically.
我目前正在尝试实现一个 Swing 组件,从JLabel它继承应该只是表示一个可以垂直定向的标签。
Beginning with this:
从这个开始:
public class RotatedLabel extends JLabel {
public enum Direction {
HORIZONTAL,
VERTICAL_UP,
VERTICAL_DOWN
}
private Direction direction;
I thought it's be a nice idea to just alter the results from getPreferredSize():
我认为只是改变结果是一个好主意getPreferredSize():
@Override
public Dimension getPreferredSize() {
// swap size for vertical alignments
switch (getDirection()) {
case VERTICAL_UP:
case VERTICAL_DOWN:
return new Dimension(super.getPreferredSize().height, super
.getPreferredSize().width);
default:
return super.getPreferredSize();
}
}
and then simply transform the Graphicsobject before I offload painting to the original JLabel:
然后Graphics在我将绘画卸载到原始对象之前简单地转换对象JLabel:
@Override
protected void paintComponent(Graphics g) {
Graphics2D gr = (Graphics2D) g.create();
switch (getDirection()) {
case VERTICAL_UP:
gr.translate(0, getPreferredSize().getHeight());
gr.transform(AffineTransform.getQuadrantRotateInstance(-1));
break;
case VERTICAL_DOWN:
// TODO
break;
default:
}
super.paintComponent(gr);
}
It seems to work—somehow—in that the text is now displayed vertically. However, placement and size are off.
它似乎有效——不知何故——因为文本现在垂直显示。但是,位置和大小已关闭。
Actually, the width of the background (orange in this case) is identical with the height of the surrounding JFramewhich is ... not quite what I had in mind.
实际上,背景的宽度(在这种情况下为橙色)与周围的高度相同JFrame……这与我的想法不太一样。
Any ideas how to solve that in a proper way? Is delegating rendering to superclasses even encouraged?
任何想法如何以适当的方式解决这个问题?甚至鼓励将渲染委托给超类吗?
回答by Joey
I got it to work now with a little help of a coworker. Basically I now have a field that indicates whether to swap height/width which is only active for the time when the original JLabeldoes its painting.
我现在在同事的帮助下开始工作。基本上我现在有一个字段来指示是否交换高度/宽度,该字段仅在原始JLabel绘画时有效。
private boolean needsRotate;
@Override
public Dimension getSize() {
if (!needsRotate) {
return super.getSize();
}
Dimension size = super.getSize();
switch (getDirection()) {
case VERTICAL_DOWN:
case VERTICAL_UP:
return new Dimension(size.height, size.width);
default:
return super.getSize();
}
}
@Override
public int getHeight() {
return getSize().height;
}
@Override
public int getWidth() {
return getSize().width;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D gr = (Graphics2D) g.create();
switch (getDirection()) {
case VERTICAL_UP:
gr.translate(0, getSize().getHeight());
gr.transform(AffineTransform.getQuadrantRotateInstance(-1));
break;
case VERTICAL_DOWN:
gr.transform(AffineTransform.getQuadrantRotateInstance(1));
gr.translate(0, -getSize().getWidth());
break;
default:
}
needsRotate = true;
super.paintComponent(gr);
needsRotate = false;
}
回答by Demi Ben-Ari
I don't know if it is relevant by now, But while searching for the same thing, i found a very good implementation in the web, http://macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html
我现在不知道它是否相关,但是在搜索相同的东西时,我在网上找到了一个非常好的实现, http://macdevcenter.com/pub/a/mac/2002/03/22/垂直文本.html
Check it out, it is an implementation over the TabbedPane with vertical text, See if it suites you purposes.
检查一下,它是在带有垂直文本的 TabbedPane 上的实现,看看它是否适合您的目的。
回答by Matthew Wise
I've had a play with this, it wasn't working very well initially because the label bounds were exactly square and caused components to the right of the label to shift and become obscured. But I then realised that it was because I am using JGoodies FormLayout. If you use this layout manager, make sure you set the column size to "preferred" not "default". HTH.
我玩过这个,它最初工作得不是很好,因为标签边界正好是正方形,并导致标签右侧的组件移动并变得模糊。但后来我意识到这是因为我使用的是 JGoodies FormLayout。如果您使用此布局管理器,请确保将列大小设置为“首选”而不是“默认”。哈。
回答by John Gardner
I think it is off because you are translating about the wrong point.
我认为这是关闭的,因为您正在翻译错误的观点。
the size of the object depends on what container you have this in, so while your preferred size might be what you want, your actual size isn't?
对象的大小取决于你把它放在什么容器里,所以虽然你喜欢的大小可能是你想要的,但你的实际大小不是?
if you have this label in the CENTER of a BorderLayout, the size is always the full size of the container (minus north+south height, minus east+west width)
如果您在 BorderLayout 的 CENTER 中有此标签,则大小始终为容器的完整大小(减去北 + 南高,减去东 + 西宽度)
so don't you have to translate about the actual size, not preferred size?
所以你不必翻译实际尺寸,而不是首选尺寸吗?

