Java – how to centrally print statement text?

So I'm developing my java project, and as part of the program, I'm printing text

Example:

public static void main(String[] args)
{
System.out.println("Hello");
}

Solution

Answer quickly

You can use the javacurves library to perform interesting operations on the console Read below. It's there

Before you finish, let's answer your whole question

This is a novice question:) but this is an effective question So here are some tips for you:

The first question is how wide is the terminal? (calculated by the number of characters) the fixed size of the old terminal is 80 characters and 25 lines;

So as a first step, assume that it is 80 characters wide

How would you put a string on an 80 character terminal screen?

Do you need to worry about the length of the string? How do you position horizontally? Do you want to add spaces? Is there a format string you can think of?

Once you have written a program, you can give it any string that will correctly display these assumptions (the terminal is 80 characters wide). You can now start to worry about what will happen if you connect to a more or less terminal, more than 80 characters? Or whether you are connected to the terminal For example, if you don't "realize" whether your code makes sense? Probably not

So the question is how do you get all this information?

What you require is the ability to treat the control desk as an intelligent telex (TTY) terminal with character based control function The old telex terminal can do many interesting things

Some history

Telecommunication terminals are complex things, from many legacy terminal manufacturers (IBM, Dec, etc.)... These telex terminals have been developed to solve many problems, such as being able to display content remotely from mainframes and minicomputers

In the history of terminal development, there are many terminal standards VT100, vt200, vt220, ANSI and so on

These terminals can locate cursors and windows and colors, highlight text, underline, etc., but not everyone can do everything But this is done using the "control" character CTRL - L clears the screen on ANSI and VT terminals, but may be provided by other pages

If you write a specific program, it makes no sense elsewhere Therefore, the need to make this simple has led to the development of several abstract libraries, which will hide malice

The first was called termcap (terminal function) library, which provided a general way to deal with terminals on UNIX systems around 1978 It can tell the terminal running the program about the available functions (e.g. the ability to change the color of the text), or position the cursor at a certain position, or clear itself, etc., and then the program will modify its behavior accordingly

The second library was called the curse, about 1985 It was developed as part of the BSD system for writing games... One of the most popular versions of this library is the GNU curses Library (formerly known as ncurses)

On VMS, I believe this library is called SMG $(screen management Library)

With the answer

Any how, so you can use one of these libraries in Java to determine whether you are working on the correct terminal The source code has a library called javacurves, which can provide this function for Java programs This will be an exercise to learn how to use the new library in your program, which should be exciting

Javacurves provides terminal programming function in UNIX and windows environment It will be an interesting sport to see if you can use it to play

Advanced sports

Another exercise is to use the same library to see if you can create a program that displays well on the terminal, and write a text file without terminal code;

If you have any questions, please email and I will help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>