尽管有很多尝试,但我无法获得我想看到的结果 - 以jlabel和jlabel为中心的文字在边界lay派中有点集中。我说“有点”,因为窗户的右角也应该有另一个标签“状态”。在这里,对此负责的代码:

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);

感谢您提供的任何帮助。

有帮助吗?

解决方案

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>");

其他提示

以下构造函数, JLabel(String, int), ,允许您指定标签的水平对齐。

JLabel label = new JLabel("The Label", SwingConstants.CENTER);
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
myLabel.setVerticalAlignment(SwingConstants.CENTER);

如果由于某种原因无法重建标签,这就是您如何编辑先前的Jlabel的这些属性。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top