Hur*_*ler 32 java user-interface swing jlabel htmltext
尽管经过多次尝试,但我无法得到我想看到的结果 - 文本居中在JLabel中,而JLabel在BorderLayout中居中.我说"有点",因为窗口的右下角应该还有另一个标签"status".这里有一些代码负责:
setLayout(new BorderLayout());
JPanel area = new JPanel();
JLabel text = new JLabel(
"<html>In early March, the city of Topeka," +
" Kansas,<br>temporarily changed its name to Google..." +
"<br><br>...in an attempt to capture a spot<br>" +
"in Google's new broadband/fiber-optics project." +
"<br><br><br>source: http://en.wikipedia.org/wiki/Google_server" +
"#Oil_Tanker_Data_Center</html>", SwingConstants.CENTER);
text.setVerticalAlignment(SwingConstants.CENTER);
JLabel status = new JLabel("status", SwingConstants.SOUTH_EAST);
status.setVerticalAlignment(SwingConstants.CENTER);
Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
area.setBackground(Color.darkGray);
text.setForeground(Color.green);
// text.setAlignmentX(CENTER_ALIGNMENT);
// text.setAlignmentY(CENTER_ALIGNMENT);
// text.setHorizontalAlignment(JLabel.CENTER);
// text.setVerticalAlignment(JLabel.CENTER);
Font font2 = new Font("SansSerif", Font.BOLD, 20);
status.setFont(font2);
status.setForeground(Color.green);
area.add(text, BorderLayout.CENTER);
area.add(status, BorderLayout.EAST);
this.add(area);
Run Code Online (Sandbox Code Playgroud)
感谢您提供的任何帮助.
小智 94
以下构造函数JLabel(String, int)允许您指定标签的水平对齐方式.
JLabel label = new JLabel("The Label", SwingConstants.CENTER);
Run Code Online (Sandbox Code Playgroud)
ram*_*ey0 33
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
myLabel.setVerticalAlignment(SwingConstants.CENTER);
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因无法重建标签,则可以使用以下方法编辑预先存在的JLabel的这些属性.
Eng*_*uad 29
String text = "In early March, the city of Topeka, Kansas," + "<br>" +
"temporarily changed its name to Google..." + "<br>" + "<br>" +
"...in an attempt to capture a spot" + "<br>" +
"in Google's new broadband/fiber-optics project." + "<br>" + "<br>" +"<br>" +
"source: http://en.wikipedia.org/wiki/Google_server#Oil_Tanker_Data_Center";
JLabel label = new JLabel("<html><div style='text-align: center;'>" + text + "</div></html>");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
124231 次 |
| 最近记录: |