是否有相当于SWT的“java.awt.headless = true”?

时间:2012-08-30 08:53:15

标签: java swt headless

我正在尝试在无头环境中运行Java SWT GUI应用程序,并问自己是否有equivalent to the AWT选项-Djava.awt.headless=true

如果不是,我会选择XvfbXvnc,无论如何我很好奇。

1 个答案:

答案 0 :(得分:2)

根据这个非常古老的bug report,SWT无头模式存在一些问题。但它又是非常古老的,并且适用于Mac OS。

Q& A

  

问自己是否有相当于AWT的选项   “-Djava.awt.headless =真”?

没有这样的财产。虽然在Win7和eclipse 4.2上,你可以在无头模式下操作(只是不做shell.open())。

代码

下面的代码打开一个浏览器实例,等待其加载事件,一旦页面加载,它就会转储其页面内容并关闭shell。此外,它还会在此无头模式下创建颜色和图像。

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HeadLess 
{
    public static void main(String[] args) {
        new HeadLess().start();
    }

    public void start()
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, true));
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        shell.setLayoutData(gridData);

        // Create browser and listen for events in headless mode
        final Browser browser = new Browser(shell, SWT.NONE);
        gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        browser.setLayoutData(gridData);

        browser.setUrl("https://stackoverflow.com/questions/12193231/is-there-an-equivalent-to-java-awt-headless-true-for-swt");
        browser.addProgressListener(new ProgressListener() {
            public void completed(ProgressEvent event) {
                System.out.println(browser.getText());
                shell.close();
            }
            public void changed(ProgressEvent event) {
            }
        });

        browser.addLocationListener(new LocationListener() {
            public void changing(LocationEvent event) {
                System.out.println(event.location);
            }
            public void changed(LocationEvent event) {
            }
        });

        // Creating image in headless mode !!
        Color red = new Color(display, new RGB(255, 0, 0));
        Image image = new Image(display, 100, 100);
        GC gc = new GC(image);
        gc.setBackground(red);
        gc.fillRectangle(image.getBounds());
        gc.dispose();

        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] {image.getImageData()};
        loader.save("swt.png", SWT.IMAGE_PNG);

        red.dispose();
        image.dispose();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }
}

其他

  1. swt browser in headless mode
  2. Easiest way to unit test SWT and Swing apps in a headless environment?