superscript in Java String

后端 未结 7 1691
一向
一向 2020-12-09 18:45

Does Java String supports superscript in a String? If yes then how can I use it, I have searched the web and also the API but not able to figure out how I can use it for my

相关标签:
7条回答
  • 2020-12-09 19:44

    Here is the example how to superscript the string "JavaTM" in Java.

    in as1.addAttribute method '4' is the beginIndex Index of the first character and '6' is the endIndex Index of the character for superscript.

    Example:

    public class TextAttributesSuperscript extends JPanel {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
        AttributedString as1 = new AttributedString("JavaTM");
        as1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 4, 6);
        g2d.drawString(as1.getIterator(), 15, 60);
    }
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("Java Superscript Example");
        frame.add(new TextAttributesSuperscript());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        frame.setSize(320, 190);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    

    }

    Output of this program:

    enter image description here

    0 讨论(0)
提交回复
热议问题