2017-04-05 1 views
0

私はJTextAreaを編集できません。 コンソールとして動作しますが、入力はありません。私は、次回の追加のためだけに背景色を変更したいが、どのように考えていない。私は私の次のアペンド呼び出す直前にJTextAreaは次にappendの背景色を変更します

  1. は、タイプフォントのインスタンスを作成して、何とかこのフォントオブジェクト
  2. 呼び出し方法JTextArea.setFont(私は以前に作成されたインスタンス)の背景色を設定します。私はアイデアを持っています。
  3. JTextArea.append( "バックグラウンドカラーのメッセージ\ n")を呼び出します。

私はそれが動作すると思いますが、私はどのようにフォントオブジェクトのBackGroundColor属性を設定するのか分かりません。誰かが私にいくつかの洞察をお願いできますか?ありがとう。

答えて

1

JTextAreaは使用できません。異なるフォントの色はサポートしていません。

代わりにJTextPaneを使用する必要があり、アトリビュートで再生することができます。ここでは、始めるための簡単な例です:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class TextPaneAttributes extends JPanel 
{ 

    public TextPaneAttributes() 
    { 
     setLayout(new BorderLayout()); 

     JTextPane textPane = new JTextPane(); 
     textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight"); 

//  DefaultHighlighter highlighter = (DefaultHighlighter)textPane.getHighlighter(); 
//  highlighter.setDrawsLayeredHighlights(false); 

     // Define some character and paragraph attributes 

     SimpleAttributeSet keyWord = new SimpleAttributeSet(); 
     StyleConstants.setBold(keyWord, true); 

     SimpleAttributeSet green = new SimpleAttributeSet(); 
     StyleConstants.setForeground(green, Color.GREEN); 

     SimpleAttributeSet center = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); 

     SimpleAttributeSet left = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT); 

     // Change attributes on some existing text 

     StyledDocument doc = textPane.getStyledDocument(); 
     doc.setCharacterAttributes(0, 3, keyWord, false); 
     doc.setCharacterAttributes(8, 5, green, true); 
     doc.setParagraphAttributes(20, 1 , center, false); 

     // Add some text with attributes 

     try 
     { 
      doc.insertString(doc.getLength(), "\nNormal text", null); 
      doc.insertString(doc.getLength(), "\nGreen text centered", green); 
      doc.setParagraphAttributes(doc.getLength(), 1 , center, false); 
      doc.insertString(doc.getLength(), "\nKeyword text", keyWord); 
      doc.setParagraphAttributes(doc.getLength(), 1 , left, false); 

      // Newly typed text at the end of the document will inherit the 
      // "keyword" attributes unless we remove the attributes 

      textPane.setCaretPosition(doc.getLength()); 
      textPane.getInputAttributes().removeAttributes(keyWord); 
     } 
     catch(Exception e) {} 

     // Add text pane to frame 

     JScrollPane scrollPane = new JScrollPane(textPane); 
     scrollPane.setPreferredSize(new Dimension(200, 250)); 
     add(scrollPane); 

     // Create a Button panel 

     JPanel buttons = new JPanel(); 
     add(buttons, BorderLayout.PAGE_END); 

     // Add a Bold button 

     JButton bold = new JButton(new StyledEditorKit.BoldAction()); 
     buttons.add(bold); 

     // Add Right Alignment button 

     JButton right = new JButton(new StyledEditorKit.AlignmentAction("Align Right", StyleConstants.ALIGN_RIGHT)); 
     buttons.add(right); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new TextPaneAttributes()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(() -> createAndShowGUI()); 
/* 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
*/ 
    } 
} 

私はあなたがテキストの背景色を含む、制御することができ、他のプロパティのStyleConstants APIを読んでもらおう。各属性セットに複数のプロパティを設定できます。

詳細と動作例については、Text Component FeaturesのSwingチュートリアルのセクションをお読みください。

+0

ありがとうございます! TextPaneに変更され、すべてが良好です! – noobcoder

関連する問題