当前位置: 首页 > 面试题库 >

如何在JLabel中垂直显示文本?(Java 1.6)

隗新霁
2023-03-14
问题内容

我希望在JLabel中垂直显示文本,第一个字母在底部,最后一个字母在顶部。这可能吗?


问题答案:

我找到了以下页面:http
:
//www.java2s.com/Tutorial/Java/0240__Swing/VerticalLabelUI.htm。

我不知道您是否希望字母彼此“站立”或全部旋转。

/*
 * The contents of this file are subject to the Sapient Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://carbon.sf.net/License.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is The Carbon Component Framework.
 *
 * The Initial Developer of the Original Code is Sapient Corporation
 *
 * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
 */


import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.plaf.basic.BasicLabelUI;

/**
 * This is the template for Classes.
 *
 *
 * @since carbon 1.0
 * @author Greg Hinkle, January 2002
 * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:27 $)
 * @copyright 2002 Sapient
 */

public class VerticalLabelUI extends BasicLabelUI {
    static {
        labelUI = new VerticalLabelUI(false);
    }

    protected boolean clockwise;


    public VerticalLabelUI(boolean clockwise) {
        super();
        this.clockwise = clockwise;
    }


    public Dimension getPreferredSize(JComponent c) {
        Dimension dim = super.getPreferredSize(c);
        return new Dimension( dim.height, dim.width );
    }

    private static Rectangle paintIconR = new Rectangle();
    private static Rectangle paintTextR = new Rectangle();
    private static Rectangle paintViewR = new Rectangle();
    private static Insets paintViewInsets = new Insets(0, 0, 0, 0);

    public void paint(Graphics g, JComponent c) {

        JLabel label = (JLabel)c;
        String text = label.getText();
        Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

        if ((icon == null) && (text == null)) {
            return;
        }

        FontMetrics fm = g.getFontMetrics();
        paintViewInsets = c.getInsets(paintViewInsets);

        paintViewR.x = paintViewInsets.left;
        paintViewR.y = paintViewInsets.top;

        // Use inverted height & width
        paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
        paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

        String clippedText =
            layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

        Graphics2D g2 = (Graphics2D) g;
        AffineTransform tr = g2.getTransform();
        if (clockwise) {
            g2.rotate( Math.PI / 2 );
            g2.translate( 0, - c.getWidth() );
        } else {
            g2.rotate( - Math.PI / 2 );
            g2.translate( - c.getHeight(), 0 );
        }

        if (icon != null) {
            icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
        }

        if (text != null) {
            int textX = paintTextR.x;
            int textY = paintTextR.y + fm.getAscent();

            if (label.isEnabled()) {
                paintEnabledText(label, g, clippedText, textX, textY);
            } else {
                paintDisabledText(label, g, clippedText, textX, textY);
            }
        }

        g2.setTransform( tr );
    }
}


 类似资料:
  • 问题内容: 我试图找到最有效的方法来使div对齐文本。我尝试了几件事,但似乎都没有用。 问题答案: 对于CSS 2浏览器,可以使用/ 来使内容居中。 jSFiddle提供了一个示例: 可以将旧浏览器(Internet Explorer 6/7)的hack合并为样式,并用于从较新的浏览器中隐藏样式:

  • 问题内容: 目标:纯Canvas上的Android> = 1.6。 假设我想编写一个函数,该函数将绘制一个(宽度,高度)大的红色矩形,然后在其中绘制一个黑色的 Hello World 文本。我希望文本在视觉上位于矩形的中心。因此,让我们尝试: 现在,我不知道要在drawText的参数中加上标记的内容,即我不知道如何垂直对齐文本。 就像是 ???? = topLeftY + height / 2 +

  • 即使SWT表格为空,是否可以始终在表格中显示垂直滚动条?通过始终显示(可能已禁用)垂直滚动条,可以避免最后一列在使用列权重数据(ColumnWeightData)进行布局时部分隐藏。 我试图用SWT初始化表。V\u滚动或使用表格。getVerticalBar()。setVisible(true)-两者均未成功。 在ScrollableComposite中有一个方法设置总是显示滚动条。我要找的是表中

  • 问题内容: 我有一个显示在JLabel中的HTML表(带有CSS的样式)。我希望单元格的内容(单行文本)水平和垂直居中。水平居中很容易,但是我似乎无法使文本居中。我已经尝试过并同时使用了和参数。我已经看了几个技巧,但是似乎都没有用,而我尝试过的那些没有用。 我现在所拥有的: 内联(在标记中)CSS: HTML的相关部分: 问题答案: 支持 在Swing组件的HTML 被限制到3.2,但应该工作。

  • 我想使用flexbox来垂直对齐 中的一些内容,但没有取得很大的成功。 我在网上查了一下,很多教程实际上都使用了包装器div,它从父级的flex设置中获得,但我想知道是否可以去掉这个附加元素? 我选择在此实例中使用flexbox,因为列表项高度将是动态的。 null null

  • 问题内容: Java可以显示png,jpg和其他图片格式,但是我必须通过获取文件路径在JLable中显示bmp文件。 ImageIcon支持典型图像。 在我正在工作的项目中,我无法打开bmp文件并以jpg格式存储同一文件,因为我不允许在运行时存储某些内容。我只能将图像保存在内存中。但是我不知道该怎么做。 我怎样才能显示在 ? 谢谢 问题答案: 我发现一些用Java 1.5编写的类,但是您可以轻松更