java普通类转javafx程序
只需要集成application 就可以了
import javafx.application.application;
import javafx.scene.scene;
import javafx.scene.control.alert;
import javafx.scene.control.button;
import javafx.scene.layout.stackpane;
import javafx.stage.stage;
public class test extends application {
public static void main(string[] args) {
launch();
}
@override
public void start(stage primarystage) throws exception {
// 设置舞台标题
primarystage.settitle("hello world app");
// 创建按钮
button button = new button("click me");
// 设置按钮点击事件处理逻辑
button.setonaction(event -> {
// 创建对话框
alert alert = new alert(alert.alerttype.information);
alert.settitle("hello");
alert.setheadertext(null);
alert.setcontenttext("hello, world!");
// 显示对话框
alert.showandwait();
});
// 创建布局并将按钮添加到布局中
stackpane layout = new stackpane();
layout.getchildren().add(button);
// 创建场景并将布局设置为场景的根节点
scene scene = new scene(layout, 300, 200);
// 将场景设置到舞台
primarystage.setscene(scene);
// 显示舞台
primarystage.show();
}
}
java、javafx判断程序是否已运行
package com.test.util.sys;
import java.io.file;
import java.io.randomaccessfile;
import java.nio.channels.filelock;
/**
* 单个实例程序检测
* @author luohui
* @create 2020/8/27
* @since 1.0.0
*/
public class singleappcheckutil {
/**
* 检测是否有其他实例在运行
*/
public static void check() {
try {
file file = new file(system.getproperty("java.io.tmpdir") + "singletest.single");
if(!file.exists()) {
file.createnewfile();
}
randomaccessfile accessfile = new randomaccessfile(file, "rw");
filelock lock = accessfile.getchannel().trylock();
if(lock == null) {
system.out.println("程序已运行,退出");
system.exit(0);
}
} catch (exception e) {
tools.savelog(e);
system.exit(0);
}
}
}
package com.test.main;
import com.test.util.sys.singleappcheckutil;
import javafx.application.application;
import javafx.stage.stage;
import java.net.url;
import java.util.timer;
/**
* javafx首页启动
* @author luohui
* @create 2020/4/13
* @since 1.0.0
*/
public class main extends application {
public static void main(string[] args) {
launch(args);
}
@override
public void init() {
//检测是否有已运行程序
singleappcheckutil.check();
}
@override
public void start(stage primarystage) throws exception {
//业务逻辑代码
//...
}
}
package com.test.main;
/**
* java程序首页启动
* @author luohui
* @create 2020/4/13
* @since 1.0.0
*/
public class main1 {
public static void main(string[] args) {
//检测是否有已运行程序
singleappcheckutil.check();
//业务逻辑代码
//...
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论