2017-12-11 17 views
0

Stringを作成して、時刻を10h 30minという形式で表示したいが、単位(hとmin)は数字よりも小さいフォントでなければならない。 JLabelを使用する場合、私はスパン属性を持つhtml形式の文字列でこの作業を行います。JavaスイングのHTMLフォーマットの文字列

これで、このようなStringをカスタムオブジェクトに追加し、drawAlignedStringメソッドで書きたいと思います。しかし、ここでhtmlの渡しは機能しません。カスタムオブジェクトは、コードを表示し、フォーマットされたStringは表示しません。

別の部分文字列を使用して文字列を描画するために、この作業または他の解決策を得る方法はありますか?

これは私が試したものです:ラベルが設定されている

String time = String.format(
      "<html>%d<span style=\"font-family:Arial Unicode MS;font-size:12px;\">h </span> %d<span " 
        + "style=\"font-family:Arial Unicode MS;font-size:12px;\">min</span></html>", 
      absSeconds/3600, (absSeconds % 3600)/60); 
    g2.setFont(this.centerTextFont); 
    g2.setPaint(this.centerTextColor); 
    TextUtilities.drawAlignedString(time, g2, (float) area.getCenterX(), (float) area.getCenterY(), 
      TextAnchor.CENTER); 
+0

ご質問・ショー

enter image description here [試み](// idownvotedbecau.se/noattempt/)問題を解決します。試したことがある場合は、私たちの質問を編集して、あなたがしたことを正確に詳述し、調査し、参考になったがあなたの質問に答えなかったリンクを指すようにしてください。ソリューションをコーディングする場合は、そのコードを編集に追加する必要があります。あなたの試行は[MCVE](// stackoverflow.com/help/mcve)に変えて読んで理解してください。また、[Stack Overflow question checklist](// meta.stackoverflow.com/questions/260648)を参照してください。 –

+0

@chade_私が使用したコードを追加しました。 –

答えて

1

たら、ラベルのGraphicsからpaint方法を渡します。

import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 

public class LabelRenderTest { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

      String title = "<html><body style='width: 200px; padding: 5px;'>" 
       + "<h1>Do U C Me?</h1>" 
       + "Here is a long string that will wrap. " 
       + "The effect we want is a multi-line label."; 

       JFrame f = new JFrame("Label Render Test"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       BufferedImage image = new BufferedImage(
        400, 
        300, 
        BufferedImage.TYPE_INT_RGB); 
       Graphics2D imageGraphics = image.createGraphics(); 
       GradientPaint gp = new GradientPaint(
        20f, 
        20f, 
        Color.red, 
        380f, 
        280f, 
        Color.orange); 
       imageGraphics.setPaint(gp); 
       imageGraphics.fillRect(0, 0, 400, 300); 

       JLabel textLabel = new JLabel(title); 
       textLabel.setSize(textLabel.getPreferredSize()); 

       Dimension d = textLabel.getPreferredSize(); 
       BufferedImage bi = new BufferedImage(
        d.width, 
        d.height, 
        BufferedImage.TYPE_INT_ARGB); 
       Graphics g = bi.createGraphics(); 
       g.setColor(new Color(255, 255, 255, 128)); 
       g.fillRoundRect(
        0, 
        0, 
        bi.getWidth(f), 
        bi.getHeight(f), 
        15, 
        10); 
       g.setColor(Color.black); 
       textLabel.paint(g); 
       Graphics g2 = image.getGraphics(); 
       g2.drawImage(bi, 20, 20, f); 

       ImageIcon ii = new ImageIcon(image); 
       JLabel imageLabel = new JLabel(ii); 

       f.getContentPane().add(imageLabel); 
       f.pack(); 
       f.setLocationByPlatform(true); 

       f.setVisible(true); 
      } 
     }); 
    } 
}