[英]java swing radio buttons with vertical text
Trying to get radio buttons that look like this in java with swing: 试图通过swing获取在Java中看起来像这样的单选按钮:
* * *
O A O
F U N
F T
O
...where the *
is the radio button ...其中
*
是单选按钮
The letters can either be right side up or on their side, I don't really care. 这些字母可以是右上方,也可以是侧面,我不在乎。 I just want the buttons at the top and the letters below each button in a narrow column.
我只希望顶部的按钮和每个按钮下面的字母在狭窄的栏中。
Tried rotating with graphics in the paint()
method but it messes up when I mouse over it, redrawing the buttons without rotation. 尝试在
paint()
方法中与图形一起旋转,但是当我将鼠标悬停在其上时会弄乱,重新绘制按钮而不旋转。 Seems what I need is something like BoxLayout but that will also rotate by 90 deg. 似乎我需要的是类似BoxLayout的东西,但它也会旋转90度。 Or maybe another layer between the JPanel and the buttons.
或者在JPanel和按钮之间的另一层。
public class OffAutoOn extends JPanel {
public OffAutoOn ()
{
JRadioButton b1 = new JRadioButton ("OFF");
JRadioButton b2 = new JRadioButton ("AUTO");
JRadioButton b3 = new JRadioButton ("ON");
ButtonGroup bg = new ButtonGroup ();
bg.add (b1);
bg.add (b2);
bg.add (b3);
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (b1);
add (b2);
add (b3);
}
@Override
public void paint (Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
AffineTransform at = g2d.getTransform ();
try {
int w = getWidth ();
int h = getHeight ();
g2d.rotate (Math.PI / 2, w / 2, h / 2);
super.paint (g);
} finally {
g2d.setTransform (at);
}
}
}
You can use HTML to create vertical text for the button and then you can change the default position of the text relative to the icon. 您可以使用HTML为按钮创建垂直文本,然后可以更改文本相对于图标的默认位置。
For example: 例如:
String text = "<html>A<br>u<br>t<br>o</html";
JRadioButton button = new JRadioButton( text );
button.setHorizontalTextPosition(JRadioButton.CENTER);
button.setVerticalTextPosition(JRadioButton.BOTTOM);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.