如何确定是否存在GraphicsEnvironment中 [英] How to determine if GraphicsEnvironment exists

查看:1567
本文介绍了如何确定是否存在GraphicsEnvironment中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要用户输入密码的应用程序。

I have an application that needs user input for password.

我想要做的就是无论是读从控制台的密码(如果操作系统支持一个例如UNIX)或显示的JOptionPane,并要求用户输入他的密码(如果操作系统支持图形界面,例如窗口)。

What i want to do is to either read the password from console (if the OS supports one e.g unix) or display a JOptionPane and ask the user to enter his password (if the OS supports graphical interface e.g windows).

有些人可能会认为,一个控制台总是会在上述两种情况下可用,因此一个控制台输入就足够了。但问题是,如果在的Java应用程序开始使用的javaw.exe那么控制台不可用。因此,我需要一种方法来确定,如果我能做到这两种情况下。

Some people may argue that a console will always be available in both the above cases so a console input will be sufficient. But problem is if the Java app starts using javaw.exe then a console is not available. Thus, i need a way to determine if i can do either case.

我的问题是如何确定是否该应用程序从支撑运行的环境或一台或一图形界面。

My problem is how to determine if the environment that the application runs from supports either a console or a graphical interface.

我知道,一个静态方法存在
     GraphicsEnvironment.isHeadless()
但是从Java文档我认为这种方法无法区分如果操作系统支持的图形,但不是如果操作系统可以支持I / O设备(键盘,鼠标,屏幕)中的一个。

I know that a static method exists GraphicsEnvironment.isHeadless() but from the Java doc i think that this method cannot distinguish if the OS supports graphics, but rather than if the OS can support one of the I/O devices (keyboard, mouse, screen).

有谁知道更多吗?我能够检索如果操作系统支持控制台或图形环境?

Does anyone know more about this? Am i able to retrieve if the OS supports console or graphics environment?

先谢谢了。

推荐答案

GraphicsEnvironment.isHeadless()将返回真正的情况下:


  • 系统属性 java.awt.headless 已设置为真正

  • 您在UNIX / Linux系统上运行,并且没有显示环境变量设置

  • the system property java.awt.headless has been set to true
  • your are running on a Unix/Linux system and there is no DISPLAY environment variable set

下面是用于检索无头属性的code:

Here is the code that is used to retrieve the headless property:

    String nm = System.getProperty("java.awt.headless");

    if (nm == null) {
        /* No need to ask for DISPLAY when run in a browser */
        if (System.getProperty("javaplugin.version") != null) {
            headless = defaultHeadless = Boolean.FALSE;
        } else {
            String osName = System.getProperty("os.name");
            headless = defaultHeadless =
                Boolean.valueOf(("Linux".equals(osName) || "SunOS".equals(osName)) &&
                                (System.getenv("DISPLAY") == null));
        }
    } else if (nm.equals("true")) {
        headless = Boolean.TRUE;
    } else {
        headless = Boolean.FALSE;
    }

如果你想知道是否有任何可用的屏幕,你可以调用 GraphicsEnvironment.getLocalGraphicsEnvironment()。getScreenDevices()返回所有可用的屏幕。

If you want to know if there is any screen available, you can invoke GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() which returns all the available screens.

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;

public class TestHeadless {

    private static boolean isReallyHeadless() {
        if (GraphicsEnvironment.isHeadless()) {
            return true;
        }
        try {
            GraphicsDevice[] screenDevices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
            return screenDevices == null || screenDevices.length == 0;
        } catch (HeadlessException e) {
            e.printStackTrace();
            return true;
        }
    }

}

这篇关于如何确定是否存在GraphicsEnvironment中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆