-1

doc.insertString(doc.getLength(), text + "\n", keyWord); 「§」のすべてのインスタンスと数字(「§1」など)を色に置き換え、その後のコード自体を削除し、テキストを色付けしたままにする必要があります。コードは色付けして削除できますが、問題は、実際のドキュメント (テキストペイン) からテキストを削除すると、ローカル変数が更新されず、それを行う最善の方法がわからないことです。 be (ローカル変数は「テキスト」)。これに関する問題は、コードではないものを削除し始めることです。これは、間違ったインデックスが原因であると思います。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;

import javax.swing.JFrame;
import java.awt.geom.Ellipse2D;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;
import javax.swing.JScrollPane;

import me.woder.bot.Client;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TorchGUI extends JPanel{
  private static final long serialVersionUID = 1L;
  public JFrame frame;
  private JTextField textField;
  Client c;
  JTextPane chat;
  JTextArea status;

  final StyleContext cont = StyleContext.getDefaultStyleContext();
  final AttributeSet black = cont.addAttribute(cont.getEmptySet(),     StyleConstants.Foreground, new Color(0,0,0));
  final AttributeSet blue = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,0,170));
  final AttributeSet green = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,170,0));
  final AttributeSet dark_aqua = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,170,170));
  final AttributeSet dark_red = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(170,0,0));
  final AttributeSet purple = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(170,0,170));
  final AttributeSet orange = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,170,0));
  final AttributeSet grey = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(170,170,170));
  final AttributeSet dark_grey = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,85,85));
  final AttributeSet indigo = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,85,255));
  final AttributeSet bright_green = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,255,85));
  final AttributeSet aqua = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,255,255));
  final AttributeSet red = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,85,85));
  final AttributeSet pink = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,85,255));
  final AttributeSet yellow = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,255,85));
  final AttributeSet white = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,255,255));
  final AttributeSet reset = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.black);
  final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
  DefaultStyledDocument doc = new DefaultStyledDocument() {
    private static final long serialVersionUID = 1L;

  };

/** launch it up
 * 
 */public static void main(String[] args){
     TorchGUI window;
     window = new TorchGUI();
     window.frame.setVisible(true);
     window.addText("§0this should be black §1this should be blue");
  }


/**
 * Create the application.
 */
public TorchGUI(/*Client c*/) {
    //this.c = c;
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame("TorchBot 2.1");
    frame.setBounds(100, 100, 944, 555);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 520, 454);
    frame.getContentPane().add(scrollPane);

    chat = new JTextPane(doc);
    scrollPane.setViewportView(chat);
    chat.setEditable(false);


    textField = new JTextField();
    textField.setBounds(10, 476, 447, 33);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    status = new JTextArea();
    status.setBounds(540, 250, 262, 215);
    frame.getContentPane().add(status);
    status.setEditable(false);

    JTextArea textArea_2 = new JTextArea();
    textArea_2.setBounds(540, 12, 262, 228);
    frame.getContentPane().add(textArea_2);

}

public void addText(String text){
    SimpleAttributeSet keyWord = new SimpleAttributeSet();
    try {
        int len = doc.getLength();
        doc.insertString(len, text + "\n", keyWord);
        formatColour(text, len);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

public void formatColour(String text, int offset){
    System.out.println("Text: " + text + " length + " + text.length() + " offset is: " + offset);
    int before = offset;
    if (before < 0) before = 0;
    int after = text.length();
    int wordL = before;
    int wordR = before;

    while (wordR < after) {
        /*boolean is = true;
        if (wordR == after || is) {*/
       try{
            System.out.println("Now looking at:" + text.substring(wordL, wordR) + " wordR is: " + wordR + " and offset is: " + offset + " worldL is: " + wordL);
          if(text.substring(wordL, wordR).matches("§") && text.length() >= (wordR+1-offset)){
            if (text.substring(wordL, wordR+1).contains("0")){
                doc.setCharacterAttributes(wordL, text.length(), black, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("1")){
                doc.setCharacterAttributes(wordL, text.length(), blue, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("2")){                        
                doc.setCharacterAttributes(wordL, text.length(), green, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("3")){
                doc.setCharacterAttributes(wordL, text.length(), dark_aqua, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("4")){
                doc.setCharacterAttributes(wordL, text.length(), dark_red, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("5")){
                doc.setCharacterAttributes(wordL, text.length(), purple, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("6")){
                doc.setCharacterAttributes(wordL, text.length(), orange, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("7")){
                doc.setCharacterAttributes(wordL, text.length(), grey, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("8")){
                doc.setCharacterAttributes(wordL, text.length(), dark_grey, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("9")){
                doc.setCharacterAttributes(wordL, text.length(), indigo, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("a")){
                doc.setCharacterAttributes(wordL, text.length(), bright_green, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("b")){
                doc.setCharacterAttributes(wordL, text.length(), aqua, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("c")){
                doc.setCharacterAttributes(wordL, text.length(), red, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("d")){
                doc.setCharacterAttributes(wordL, text.length(), pink, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("e")){
                doc.setCharacterAttributes(wordL, text.length(), yellow, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("f")){
                doc.setCharacterAttributes(wordL, text.length(), white, false);
                doc.remove(wordL, 2);
            }else if(text.substring(wordL, wordR+1).contains("r")){
                doc.setCharacterAttributes(wordL, text.length(), reset, false);
                doc.remove(wordL, 2);
            }else{
                doc.setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                doc.remove(wordL, 1);
            }            
        }
      wordL = wordR;  
      wordR++;
     } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
}

}

助けてくれてありがとう。

4

1 に答える 1

1

ドキュメントに追加する前に、テキストを解析します。

if/else ステートメントも削除する必要があります。このような構造を見ると、設計上の問題があることがわかります。

これが私が使用するアプローチです:

public void addText(String text)
{
    HashMap<String, AttributeSet> attributes = new HashMap<String, AttributeSet>();
    attributes.put("0", black);
    attributes.put("1", blue);

    String[] lines = text.split("%");

    for (int i = 1; i < lines.length; i++)
    {
        String line = lines[i];
        String key = line.substring(0, 1);
        String theText = line.substring(1);
        AttributeSet attribute = attributes.get(key);

        try
        {
            int len = doc.getLength();
            doc.insertString(len, theText, attribute);
        }
        catch (BadLocationException e)
        {
            e.printStackTrace();
        }
    }
}

addText() メソッドを呼び出すたびにマップを再構築したくないため、すべての色を定数として定義する代わりに、属性をマップに配置し、これを GUI の初期化の一部として行います。

また、「%」を検索するようにコードを変更します。他の文字が何かわからないため、特別な区切り文字に戻す必要があります。実際には、区切り値を含む定数変数を定義する必要があります。

最後に、GUI コンポーネントで setBounds() を使用しないでください。Swing は、レイアウト マネージャーで使用するように設計されています。

于 2013-09-12T01:54:01.497 に答える