2017-04-05 1 views
0

사용자가 편집 할 수없는 JTextArea가 있습니다. 콘솔로 작동하지만 입력하지 않습니다. 다음에 추가 할 배경색을 변경하고 싶지만 어떻게 될지 모릅니다. 난 내 옆에 APPEND 호출 직전에JTextArea 다음 append의 배경색 변경

  1. 타입 글꼴의 인스턴스를 생성하고 어떻게 든이 Font 오브젝트
  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의 스윙 튜토리얼 섹션을 참조하십시오.

+0

감사합니다. TextPane으로 변경되었으며 모두 훌륭합니다! – noobcoder