简体   繁体   English

Java-如何在没有Swing组件的情况下更改光标?

[英]Java - How to change cursor without Swing Component?

I have a Java application that run as a background service, ie no GUI. 我有一个作为后台服务运行的Java应用程序,即没有GUI。 But when starting it, say through a batch script, I have some preparation works to be done at the very beginning of my program (something like communicate with server). 但是,当启动它时,例如通过批处理脚本,我需要在程序的开始部分做一些准备工作(类似于与服务器通信)。

I want a busy cursor appear at this busy moment. 我希望在此繁忙的时刻出现繁忙的光标。 Is it possible to do this without using any Swing component? 是否可以在不使用任何Swing组件的情况下做到这一点? Thanks for any suggestion or alternative approach. 感谢您的任何建议或替代方法。

I'd go for a console-based "progress bar". 我会去找一个基于控制台的“进度条”。 It's possible to use Swing but it would render your solution useless in a headless environment. 可以使用Swing,但是它会使您的解决方案在无头的环境中变得毫无用处。 Drawing progress bars with characters in terminal is quite a common practice. 在终端中绘制带有字符的进度条是很常见的做法。

You can either implement it yourself or use a ready class. 您可以自己实现它,也可以使用一个就绪的类。 Just take a look at github or bitbucket and you'll find something for sure. 看看github或bitbucket,您肯定会发现一些东西。

A busy cursor could even be displayed as a caption with alternating last character (as simple as possible), with "frames" like these (rotating bar) 繁忙的光标甚至可以显示为带有最后一个字符(尽可能简单)的标题,并带有这样的“框架”(旋转条)

LOADING DATA /
LOADING DATA |
LOADING DATA \
LOADING DATA -

Implementing something like this wouldn't take much time. 实施这样的事情不会花费很多时间。 We're talking about minutes here. 我们在这里谈论分钟。

If the only thing that prevents you from using Swing is an appearing window, you can change its appearance thanks to window translucency . 如果唯一使您无法使用Swing的东西是出现的窗口,则可以通过窗口半透明来更改其外观。 This way you'd get an animated image overlaying your screen. 这样,您将获得覆盖屏幕的动画图像。 It wouldn't work in a terminal though. 不过,它在终端中无法正常工作。

If you're able to use Java AWT (Abstract Window Toolkit) I beleive you can use the setCursor of Component class to change the cursor. 如果您能够使用Java AWT(抽象窗口工具包),我相信您可以使用Component类的setCursor更改光标。

More on this : http://www.roseindia.net/java/example/java/awt/ChangeCursor.shtml 有关此的更多信息: http : //www.roseindia.net/java/example/java/awt/ChangeCursor.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM