2016-08-12 2 views
0

のためのカスタムカラーの使用に示しますその中にJTextPaneがあります。何が起こっているのアーティファクトは、カスタムメイドのカラーを使用した後JTextPaneの背景色

画像:

Image of problem

のでJTextPane背景の下の部分はOKですが、テキストの後ろのトップ1は、いくつかの問題を抱えています。

どうすれば修正できますか? JTextPaneの単純な透明な背景にカスタムカラーを使用して間違いを犯しましたか?プログラムのこの部分のための

コード:

t = new JTextPane(); 
SimpleAttributeSet style = new SimpleAttributeSet(); 
StyleConstants.setAlignment(style , StyleConstants.ALIGN_CENTER); 
StyleConstants.setForeground(style , Color.white); 
StyleConstants.setFontFamily(style, "Times new Roman"); 
StyleConstants.setFontSize(style, 20); 
StyleConstants.setBold(style, true); 
t.setParagraphAttributes(style,true); 
t.setText(" " + text.getT1().get(0).toUpperCase()); 
t.setOpaque(true); 
Color bg = new Color(0f,0f,0f,0.5f); 
t.setBackground(bg); 
t.setEditable(false); 
t.setBounds(250, 400, 300, 50); 
animation.add(t); 

答えて

5

スイングは透明な背景を適切にサポートしていません。 Swingは、setOpaque(....)プロパティに基づいてコンポーネントが完全に不透明かどうかを予測します。

透明な背景を使用する場合、透明なコンポーネントの背景が描画される前に親コンテナの背景が最初に塗りつぶされていることを確認する必要があります。

このプロセスの詳細については、Background With Transparencyをご覧ください。

あなたがコンポーネントのカスタマイズなどのコードを使用して独自の塗装を行うことができます。

JPanel panel = new JPanel() 
{ 
    protected void paintComponent(Graphics g) 
    { 
     g.setColor(getBackground()); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     super.paintComponent(g); 
    } 
}; 

panel.setOpaque(false); // background of parent will be painted first 
panel.setBackground(new Color(255, 0, 0, 20)); 
frame.add(panel); 

または、より簡単なアプローチは、上記のリンクで提供AlphaContainerクラスを使用することです。

0

は、私は同様の問題を抱えて考えます。 JTextPaneのコンテナは、透明でなく不透明にする必要があります。それ以外の場合、Swingはバックグラウンドを非常に間違って計算します。あなたはバグとして私は思うかもしれません。

関連する問題