Quantcast
Channel: OKWAVE 閲覧数の多い質問(Java/253)【本日】
Viewing all articles
Browse latest Browse all 68675

StyledDocumentの背景色の不具合

$
0
0
標記の件、JTextPaneにDefaultStyledDocumentを設定して、テキスト編集環境を 構築していますが、デフォルトでのフォントの背景色が正しく取得できません。 フォントのデフォルトの背景色は見た目には白なのですが、 取得される背景色は黒(r=0,g=0,b=0,a=255)になってしまっています。 背景色を適当に設定すると、その部分の色は正しく反映されます。 マニュアルで背景色を黒く設定しているのか、無設定で背景色が黒として取得されて いるのかの判別方法がなく困っております。 以下、サンプルコードを示します。 対処法をご存知の方がおられましたら、よろしくお願いいたします。 import java.awt.Color; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.MutableAttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; import javax.swing.text.StyledDocument; public class StyledDocumentText { public static void main(String[] args) { final StyledDocument document = new DefaultStyledDocument(new StyleContext()); JTextPane textPane = new JTextPane(document); textPane.addCaretListener( new CaretListener(){ public void caretUpdate(CaretEvent e) { // キャレット位置の背景色を取得し、コンソール出力 AttributeSet a = document.getCharacterElement(e.getDot()).getAttributes(); Color background = StyleConstants.getBackground(a); System.out.printf( " %s a=%d\n", background ,background.getAlpha() ); } } ); // テキスト挿入 try { document.insertString(0, "abcdefg", null); } catch (BadLocationException e) { e.printStackTrace(); } // マニュアルで背景色を設定 MutableAttributeSet attr = new SimpleAttributeSet(); StyleConstants.setBackground(attr, new Color(255,100,50,230) ); document.setCharacterAttributes( 2, 3, attr, false); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( textPane ); frame.setSize( 300, 300 ); frame.setVisible( true ); } }

Viewing all articles
Browse latest Browse all 68675

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>