垂直轴中间的 Java JLabel 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7602910/
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
Java JLabel text in middle of vertical axis
提问by CodeMed
I have a JLabel
that contains variable text in a certain location in my GUI. The problem is that the text gets displayed at the bottom of the space where the JLabel
is located. This does not convey to the end user the relevant information about the other contents of the GUI. Instead, I need the text of the JLabel
to be printed in the middle of the vertical axis of the JLabel
. A simplified version of my code is below. Can anyone show me how to alter it so that the text displays in the middle of the vertical axis instead of the bottom?
我有一个JLabel
包含在我的GUI某个位置可变文本。问题是文本显示在 所在空间的底部JLabel
。这不会向最终用户传达有关 GUI 其他内容的相关信息。相反,我需要将 的文本JLabel
打印在JLabel
. 我的代码的简化版本如下。谁能告诉我如何更改它以便文本显示在垂直轴的中间而不是底部?
Main.java:
主.java:
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Main");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new VerticalLabel("Hello"));
Dimension prefSize = new Dimension(400, 300);
frame.setPreferredSize(prefSize);
frame.setMinimumSize(prefSize);
frame.pack();
frame.setVisible(true);
}
}
VerticalLabel.java:
垂直标签.java:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder;
public class VerticalLabel extends JLabel {
public VerticalLabel(String labelText) {
Dimension myDim = new Dimension(15, 250);
this.setPreferredSize(myDim);
this.setHorizontalAlignment(LEFT);
this.setVerticalAlignment(CENTER);
this.setText(labelText);
this.setVerticalTextPosition(CENTER);
this.setUI(new VerticalLabelUI(false));
this.setBorder(new EtchedBorder());
}
}
回答by camickr
Hardcoding a random preferred size is not a good idea.
硬编码随机首选大小不是一个好主意。
You wrote a custom UI, so it is the responsibility of the UI to paint the text in the proper position.
您编写了一个自定义 UI,因此 UI 有责任在适当的位置绘制文本。
Instead of creating a custom UI you can use the Text Iconapproach to display vertical text. Create the label as follows:
您可以使用文本图标方法来显示垂直文本,而不是创建自定义 UI 。创建标签如下:
JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
TextIcon labelIcon = new TextIcon(label, "Hello", TextIcon.Layout.VERTICAL);
label.setIcon( vIcon );
Add the label to the CENTER of a panel using a BorderLayout and the vetical text will be centered vertically and horizontally.
使用 BorderLayout 将标签添加到面板的 CENTER,垂直和水平方向的文本将居中。
回答by Andrew Thompson
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder;
public class VerticalLabel extends JLabel{
public VerticalLabel(String labelText){
this.setHorizontalAlignment(LEFT);
this.setVerticalAlignment(CENTER);
this.setText(labelText);
this.setVerticalTextPosition(CENTER);
//this.setUI( new VerticalLabelUI(false) );
this.setBorder( new EtchedBorder() );
}
public static void main(String[] args){
// should be done on the EDT.
JFrame frame = new JFrame("Main");
frame.getContentPane().setLayout( new GridBagLayout() );
frame.getContentPane().add(new VerticalLabel("Hello"));
Dimension prefSize = new Dimension(200,150);
frame.setPreferredSize(prefSize);
frame.setMinimumSize(prefSize);
frame.pack();
frame.setVisible(true);
}
}