2017-05-12 16 views
1

복사가 가능하다는 장점이있는 자체 Label 클래스를 만들었습니다.JTextPane 배경을 투명하게 만들기

public class CopyableLabel extends JTextPane { 

private static final long serialVersionUID = -1; 

private static final Font DEFAULT_FONT; 

static 
{ 
    Font font = UIManager.getFont("Label.font"); 
    DEFAULT_FONT = (font != null) ? font: new Font("Tahoma", Font.PLAIN, 11); 
} 

public CopyableLabel() { 
    construct(); 
} 

private void construct() 
{ 
    setContentType("text/html"); 

    setEditable(false); 
    setOpaque(false); 
    setBackground(null); 
    setBorder(null); 

    putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true); 
    setFont(DEFAULT_FONT); 
} 

public CopyableLabel(String text) 
{ 
    super(); 
    construct(); 
    setText(text); 
} 

public void setFont(Font font) 
{ 
    super.setFont(font); 
    setMaximumSize(new Dimension(Short.MAX_VALUE,font.getSize()+4)); 
} 

public CopyableLabel(String title, int align) 
{ 
    super(); 
    construct(); 
    setText(title); 

    StyledDocument doc = getStyledDocument(); 
    SimpleAttributeSet center = new SimpleAttributeSet(); 
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); 
    switch(align) 
    { 
     case JLabel.LEFT: 
      StyleConstants.setAlignment(center, StyleConstants.ALIGN_LEFT); 
      break; 
     case JLabel.RIGHT: 
      StyleConstants.setAlignment(center, StyleConstants.ALIGN_RIGHT); 
      break; 
    } 
    doc.setParagraphAttributes(0, doc.getLength(), center, false); 
} 

문제는 Nimbus Look and Feel에서 흰색 배경이보기 흉한 것입니다. 그래서 나는 배경을 투명하게 할 수있는 가능성을 찾는다.

해결책이 있습니까?

+0

당신이 그것을 만들지 않았다, 당신은이 게시물에서 코드를 가지고 : http://stackoverflow.com/questions/997942/selecting-text-from-a-jlabel – Michael

+0

매우 유용한 게시물 –

답변

1

도움이 될 경우 도움이되지 않습니다. 그러나 Color 및 특히 색상 생성자 색상 (r, g, b, a)을 사용하는 경우 a는 투명성을 제어하는 ​​알파입니다.

따라서에 구조() 메소드를 변경 : 나는 그것을 tryed하고 JTextPane가 위의 코드를 언급에 modifyed 및 반투명 JFrame의에 넣어 둘 때 그것은 나를 위해 일한

private void construct() 
{ 
    setContentType("text/html"); 

    setEditable(false); 
    setOpaque(true); 
    backgroundColor = getBackground(); 
    int red = backgroundColor.getRed(); 
    int green = backgroundColor.getGreen(); 
    int blue = backgroundColor.getBlue(); 
    setBackground(new Color(red, green, blue, 25)); 
    //setBackground(null); 
    setBorder(null); 

    putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true); 
    setFont(DEFAULT_FONT); 
} 

.

+0

고마워. 그건 잘 작동합니다. –