2017-05-12 7 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ルックアンドフィールでは、白い背景が醜いと見えることです。だから私は背景を透明にする可能性を探します。

解決策はありますか?

+0

あなたはそれを作成していませんでした。この投稿からコードを入手しました:http://stackoverflow.com/questions/997942/selecting-text-from-a-jlabel – Michael

+0

非常に役立つ投稿 –

答えて

1

これが役立つ場合はおすすめできません。しかし、Colorを使用してください。特に、色のコンストラクタカラー(r、g、b、a)は、透明度を支配するaがアルファです。私はそれをしようと試み、あなたJTextPaneのは、上記のコードを言及し、半透明のJFrameの上に置くとmodifyed置くときそれは私のために働い

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

ありがとうございます。それはうまく動作します –