有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何显示带空格的选项卡?

我有一个java程序,在这个程序中,我用网格从JPanel上的文本文件中绘制每个字符(在它自己的框架中)

left- JPanel, right- text document 每个角色都有自己的框架,但当涉及到制表符时,就会出现问题。我尝试用8个空格替换所有制表符,但问题是(如上图所示)出现了不一致,因为制表符并不总是8个字符长有没有办法计算出一个标签使用了多少“空格”或其他关于如何获得与文本文件相同的布局的建议

绘图文本的代码:

g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Font font = new Font("monospaced", Font.PLAIN, 18);
g2.setColor(Color.BLACK);
g2.setFont(font);
String lines[] = LabAgentComponent.PASTE.split("\\r?\\n");
for (int i=0; i<lines.length; i++) {
    for(int j=0; j<lines[i].length(); j++) {
        g2.drawString(Character.toString(lines[i].charAt(j)), j * gridSize, (i+1) * gridSize);
    }
}

共 (3) 个答案

  1. # 1 楼答案

    您可以在制表符之前检查字符串的长度

    Al203的情况下,这将是5。你的标签应该从8的下一个倍数开始,中间至少有一个空格

    这里有一个小班可以帮助你:

    public class TabToSpaces
    {
        public static void main(String[] args) {
            System.out.println(replaceTab("\tb", 8, "."));
            System.out.println(replaceTab("a\tb", 8, "."));
            System.out.println(replaceTab("abcdefg\th", 8, "."));
            System.out.println(replaceTab("abcdefgh\ti", 8, "."));
            System.out.println(replaceTab("a\tb\tc\td\te", 8, "."));
            System.out.println(replaceTab("ab\tb\tc\td\te", 8, "."));
        }
    
        private static String replaceTab(String string, int tabSize, String space) {
            Pattern pattern = Pattern.compile("\t");
            Matcher matcher = pattern.matcher(string);
            StringBuffer sb = new StringBuffer();
            int offset = 0;
            while (matcher.find()) {
                int beforeLength = matcher.start() + offset;
                int spacesNeeded = (int) (Math.ceil((beforeLength + 1.0) / tabSize) * tabSize) - beforeLength;
                offset += spacesNeeded - 1;
                String spaces = new String(new char[spacesNeeded]).replace("\0", space);
                matcher.appendReplacement(sb, spaces);
            }
            matcher.appendTail(sb);
            return sb.toString();
        }
    }
    

    它输出:

    ........b
    a.......b
    abcdefg.h
    abcdefgh........i
    a.......b.......c.......d.......e
    ab......b.......c.......d.......e
    

    我用圆点让空间更清晰

  2. # 2 楼答案

    我知道怎么做我想做的。以下是代码:

    public void changeTabs(){
        String lines[] = PASTE.split("\\r?\\n");
        String together="";
        for (int i=0; i<lines.length; i++){
            for(int j=0; j<lines[i].length(); j++){
                if(Character.toString(lines[i].charAt(j)).equals("\t")){
                    int spaces= Math.abs((lines[i].indexOf("\t")%8)-8);
                    String tab= ("%-"+spaces+"s");
                    lines[i]=lines[i].replaceFirst("\t", String.format(tab, ""));
                }
            }
        }for (int i=0; i<lines.length; i++){
            together=together.concat(lines[i]+"\n");
        }
        PASTE=together;
    }
    
  3. # 3 楼答案

    Or any other suggestions on how can I get the same layout as it is in text file?

    只要用JTextAreamonospaced Font就行了。不需要定制绘画

    JTextArea甚至有一个方法,允许您控制给定选项卡的空格数

    JTextArea还有一个read(...)方法,可以将文件直接读入文本区域