Java 中的对齐文本
我们可以创建我们的类,它将扩展类 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.
相关文章
在 Java 中的 String.contains() 方法中使用正则表达式
发布时间:2023/08/10 浏览次数:125 分类:Java
-
本文将介绍如何在 Java 的 String.contains() 函数中使用正则表达式。String.contains() 函数扫描字符串中的特定字符序列。
使用 Java 创建 REST 客户端
发布时间:2023/08/10 浏览次数:129 分类:Java
-
本文将介绍创建 REST 客户端的不同工具。 第一个示例演示了使用 Spring Boot 和 OkHttp 创建简单 REST 客户端的步骤。使用 Spring Boot 创建 Java Rest 客户端的步骤 要创建 REST 客户端,IDE 需要一个框架才
使用 Java 创建 HTTPS 服务器
发布时间:2023/08/10 浏览次数:126 分类:Java
-
本文介绍如何使用 Java 创建简单的 HTTPS 服务器。使用 Java 创建 HTTPS 服务器 在用JAVA创建HTTPS服务器之前,我们必须确保已经生成了服务器将使用的Keystore和Truststore。
Java 中的 AES 256
发布时间:2023/08/10 浏览次数:83 分类:Java
-
AES 256 是一种加密和解密算法。 本教程演示如何在 Java 中实现 AES 256 来加密和解密数据。Java 中的 AES 256 AES 是一种对称加密算法,易于在软件、硬件和受限环境中实现。
在JDBC中建立连接池
发布时间:2023/08/10 浏览次数:55 分类:Java
-
JDBC 是 Sun Microsystems 的一项规范,它为 Java 应用程序提供 API 以执行不同的操作,例如对各种数据库进行读取和写入。 JDBC提供了一种数据库连接标准的语言,您可以编写数据库访问所需的程序。
在 Java 中使用 SwingUtilities.invokeLater()
发布时间:2023/08/10 浏览次数:101 分类:Java
-
SwingUtilities.invokeLater() 方法在 Abstract Window Toolkit (AWT) 事件调度线程上执行可运行对象。 我们这样做是因为 Swing 数据结构不是线程安全的。线程安全意味着它可以从多个线程并行使用而不会导致
Java 尾部调用优化
发布时间:2023/08/10 浏览次数:170 分类:Java
-
本文讨论尾部调用优化(也称为 TCO)及其在 Java 中不存在的原因。 我们还将看到一些其他可以用来在 Java 中模拟 TCO 的方法。什么是尾调用优化