【问题标题】:JTextPane print text after previous lineJTextPane 在上一行之后打印文本
【发布时间】:2014-03-31 12:15:28
【问题描述】:

我有一个带有 JTextPane 的程序。当我想在其中打印时,我的问题就来了。每次,它将在第一行打印并将其余的向下移动。 我怎样才能让它打印在文本的末尾?

我知道了:

//...

public static enum Level {
    CLASSIC,
    MAJ,
    AJOUT,
    SUPRESSION,
    CONFIG;
}

public static void add(Level level,String Message){
    switch(level){
        case CLASSIC:
            try{
                Color BLACK = new Color(0, 0, 0);
                StyleConstants.setForeground(Colors, BLACK);
                Text.insertString(0, "\n\t- Mise à jour # " + Message + " -\n\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case MAJ:
            try{
                Color ORANGE = new Color(252, 156, 51);
                StyleConstants.setForeground(Colors, ORANGE);
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case AJOUT:
            Color GREEN = new Color(58, 157, 52);
            StyleConstants.setForeground(Colors, GREEN);
            try{
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case SUPRESSION:
            Color RED = new Color(183, 19, 0);
            StyleConstants.setForeground(Colors, RED);
            try{
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case CONFIG:
            Color BLACK = new Color(0, 0, 0);
            StyleConstants.setForeground(Colors, BLACK);
            try{
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
    }
}
//...

【问题讨论】:

    标签: java swing console output jtextpane


    【解决方案1】:

    而不是做

    Text.insertString(0, Message + "\n", Colors);

    这样做

    Text.insertString(Text.getLength(), Message + "\n", Colors)

    0 是插入文本的索引位置。使用 Text.getLength(),它总是会插入到最后。

    更多信息请参见:JTextPane appending a new string

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2022-06-15
      • 2020-08-08
      • 2017-09-08
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      相关资源
      最近更新 更多