linux上前端代码一直运行,如何在Linux上使用Java实现前端

现在人们可以做出许多假设(例如.desktop文件的内容),但也许最好有一个关于如何工作的逐步指南,例如:

测试程序

此SWT程序通过runtime.getRuntime().exec启动“gedit”。

import java.io.IOException;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.events.SelectionListener;

import org.eclipse.swt.layout.RowLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

public class EditorOpener {

public static void main(String[] args) {

Display display = new Display();

Shell shell = new Shell(display);

shell.setSize(500, 200);

shell.setText("Editor Opener");

shell.setLayout(new RowLayout());

final Button button = new Button(shell, SWT.PUSH);

button.setText("open gedit");

button.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent event) {

String[] cmdArray = new String[] { "/usr/bin/gedit" };

try {

Runtime.getRuntime().exec(cmdArray);

}

catch(IOException ex) {

ex.printStackTrace();

}

}

public void widgetDefaultSelected(SelectionEvent event) {

}

});

shell.open();

while (!shell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

display.dispose();

}

}

调用Java程序

Java程序是从shell脚本调用的,它看起来像这样:

#!/usr/bin/bash

java -cp /path/to/swt.jar:/path/to/classes EditorOpener

桌面文件

指向shell文件的.desktop文件如下所示:

[Desktop Entry]

Type=Application

Terminal=false

Name=Opener

Icon=utilities-terminal

Exec='/home/stephan/Documents/opener.sh'

Categories=Application;

结果

通过点击

打开Gedit

按钮,gedit在所有其他窗口上按预期启动。

演示

测试是在CentOS Linux 7上完成的:

gnome-shell --version

给出输出:

GNOME Shell 3.28.3

mlX5n.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值