2017-12-11 3 views
0

String 형식으로 시간을 표시하려면 10h 30min이지만 단위 (h 및 min)는 숫자보다 작은 글꼴이어야합니다. JLabel을 사용할 때 span 속성이있는 html 형식의 문자열로이 작업을 수행합니다.자바 스윙 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

되면, 라벨의 Graphicspaint 방법을 전달합니다.

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); 
      } 
     }); 
    } 
} 
관련 문제