How to detect if a graphical interface is supported?

前端 未结 5 730
悲哀的现实
悲哀的现实 2021-01-01 12:20

I need my Java program to have two display modes: a GUI interface and a command line interface. If I run it in Windows, OS X, or another graphical environment I should get

5条回答
  •  时光说笑
    2021-01-01 12:41

    Both dacwe and vitaut are right. I just wanted to add one recommendation. You should use MVC (model-view-controller) pattern when you are designing your application. So, if it is running in UI mode it uses UIView, otherwise the ConsoleView.

    Dacwe recommended you how can you decide automatically which mode to use. Due to Java is cross platform language I think the name is operating system is irrelevant for you. The fact that the system has graphic environment is more relevant, so use GraphicsEnvironment.isHeadless().

    System.console() will help you to create shell controlled application.

提交回复
热议问题