迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中的对齐文本

作者:迹忆客 最近更新:2023/08/10 浏览次数:

我们可以创建我们的类,它将扩展类 Java.text.Format 来对齐文本。 本教程演示如何在 Java 中对齐文本。


Java 中的对齐文本

Format 是一个抽象基类,用于格式化数字、消息、日期等敏感信息。我们将实现名为 Text_Alignment 的类,它将扩展 Format 类,以便我们可以格式化文本以使其对齐。

此类将定义三个枚举:Center、Right 和 Left,稍后在 switch 条件中用作根据给定指令对齐文本的情况。 该类使用一行的最大字符数,然后对齐每一行。

请参阅示例。

package jiyik;

import java.text.FieldPosition;
import java.util.ArrayList;
import java.text.Format;
import java.text.ParsePosition;
import java.util.List;
import java.util.ListIterator;

public class Text_Alignment extends Format {

    private static final long serialVersionUID = 1L;

    public enum Align_Text {
        LEFT, CENTER, RIGHT,
    }

    // justification for formatting
    private Align_Text Current_Alignment;

    // maximum length of a line
    private int Maximum_Chars;

    public Text_Alignment(int Maximum_Chars, Align_Text alignment) {
        switch (alignment) {
            case LEFT:
            case CENTER:
            case RIGHT:
            this.Current_Alignment = alignment;
            break;
            default:
                throw new IllegalArgumentException("invalid justification");
    }
        if (Maximum_Chars < 0) {
            throw new IllegalArgumentException("Maximum_Chars should be positive.");
        }
        this.Maximum_Chars = Maximum_Chars;
    }

    public StringBuffer format(Object Input_Object, StringBuffer Align_Position, FieldPosition Ignore_Position) {
        String Demo = Input_Object.toString();
        List<String> Strings_List = Split_String(Demo);
        ListIterator<String> List_Iterator = Strings_List.listIterator();

        while (List_Iterator.hasNext())  {
            String Wanted_String = List_Iterator.next();

            // put the spaces in the right place.
            switch (Current_Alignment) {
                case RIGHT:
                    ALIGN(Align_Position, Maximum_Chars - Wanted_String.length());
                    Align_Position.append(Wanted_String);
                    break;

                case CENTER:
                    int toAdd = Maximum_Chars - Wanted_String.length();
                    ALIGN(Align_Position, toAdd / 2);
                    Align_Position.append(Wanted_String);
                    ALIGN(Align_Position, toAdd - toAdd / 2);
                    break;

                case LEFT:
                    Align_Position.append(Wanted_String);
                    ALIGN(Align_Position, Maximum_Chars - Wanted_String.length());
                    break;
            }
            Align_Position.append("\n");
        }
        return Align_Position;
    }

    protected final void ALIGN(StringBuffer Append_To, int Length) {
        for (int i = 0; i < Length; i++)
            Append_To.append(' ');
    }

    String format(String Demo) {
        return format(Demo, new StringBuffer(), null).toString();
    }

    // ParseObject will be required but it is not useful here.
    public Object parseObject(String Source_String, ParsePosition position) {
        return Source_String;
    }

    private List<String> Split_String(String Demo) {
        List<String> List = new ArrayList<String>();
        if (Demo == null)
          return List;
        for (int x = 0; x < Demo.length(); x = x + Maximum_Chars) {
            int End_Index = Math.min(x + Maximum_Chars, Demo.length());
            List.add(Demo.substring(x, End_Index));
        }
        return List;
    }

    public static void main(String[] args)
    {
      String Demo_Text = "Jiyik is a resource for everyone interested in programming, embedded software, and electronics."
              + "It covers the programming languages like Python, C/C++, C#, and so on in this website's first development stage."
              + "Open-source hardware also falls in the website's scope, like Arduino, Raspberry Pi, and BeagleBone."
              + "Jiyik aims to provide tutorials, how-to's, and cheat sheets to different levels of developers and hobbyists.";
      // Align Left
      Text_Alignment align = new Text_Alignment(50, Align_Text.LEFT);
      System.out.println("This is the left alignment of the given text: ");
      System.out.println( align.format(Demo_Text) );

      // Align Right
      Text_Alignment align1 = new Text_Alignment(50, Align_Text.RIGHT);
      System.out.println("This is the right alignment of the given text: ");
      System.out.println( align1.format(Demo_Text) );

      // Align Center
      Text_Alignment align2 = new Text_Alignment(50, Align_Text.CENTER);
      System.out.println("This is the center alignment of the given text: ");
      System.out.println( align2.format(Demo_Text) );


    }

}

上面的代码将给定文本设置为左对齐、右对齐和居中对齐。 查看输出:

This is the left alignment of the given text:
Jiyik is a resource for everyone interested i
n programming, embedded software, and electronics.
It covers the programming languages like Python, C
/C++, C#, and so on in this website's first develo
pment stage.Open-source hardware also falls in the
 website's scope, like Arduino, Raspberry Pi, and
BeagleBone.Jiyik aims to provide tutorials, h
ow-to's, and cheat sheets to different levels of d
evelopers and hobbyists.

This is the right alignment of the given text:
Jiyik is a resource for everyone interested i
n programming, embedded software, and electronics.
It covers the programming languages like Python, C
/C++, C#, and so on in this website's first develo
pment stage.Open-source hardware also falls in the
 website's scope, like Arduino, Raspberry Pi, and
BeagleBone.Jiyik aims to provide tutorials, h
ow-to's, and cheat sheets to different levels of d
                          evelopers and hobbyists.

This is the center alignment of the given text:
Jiyik is a resource for everyone interested i
n programming, embedded software, and electronics.
It covers the programming languages like Python, C
/C++, C#, and so on in this website's first develo
pment stage.Open-source hardware also falls in the
 website's scope, like Arduino, Raspberry Pi, and
BeagleBone.Jiyik aims to provide tutorials, h
ow-to's, and cheat sheets to different levels of d
             evelopers and hobbyists.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便