1. 先行预览版
This commit is contained in:
parent
3bec2895b1
commit
40e1932f0c
|
@ -1,5 +1,6 @@
|
|||
package org.aohe;
|
||||
|
||||
import com.formdev.flatlaf.FlatDarkLaf;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.core.sane.utils.SaneSessionUtils;
|
||||
import org.aohe.core.swing.MainWindow;
|
||||
|
@ -18,7 +19,7 @@ public class Main {
|
|||
try {
|
||||
//设置窗口主题
|
||||
//FlatLightLaf.setup();
|
||||
UIManager.setLookAndFeel(new FlatLightLaf());
|
||||
UIManager.setLookAndFeel(new FlatDarkLaf());
|
||||
} catch (UnsupportedLookAndFeelException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
package org.aohe.core.swing;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.core.web.SocketFactory;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
@Slf4j
|
||||
public class NewWindow extends JFrame {
|
||||
|
||||
private static JTextArea logTextArea;
|
||||
|
||||
public NewWindow() {
|
||||
// 设置窗口标题
|
||||
setTitle("扫描前端");
|
||||
|
||||
// 设置默认的关闭操作
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
// 设置窗口大小
|
||||
setSize(400, 300);
|
||||
|
||||
// 居中显示窗口
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
// 初始化UI组件
|
||||
initializeComponents();
|
||||
}
|
||||
|
||||
private void initializeComponents() {
|
||||
|
||||
JLabel textField = new JLabel();
|
||||
textField.setForeground(Color.GRAY);
|
||||
textField.setText("服务状态:启动中...");
|
||||
|
||||
//初始化连接服务
|
||||
initSocket();
|
||||
|
||||
// 示例:添加一个按钮,点击时向文本区域添加日志信息
|
||||
JButton stopButton = new JButton("退出");
|
||||
stopButton.setEnabled(true);
|
||||
stopButton.addActionListener(e -> {
|
||||
stopButton.setEnabled(false);
|
||||
logTextArea.append("关闭服务中...\n");
|
||||
//开个新线程关闭,防止主UI线程卡死
|
||||
stopSocket();
|
||||
textField.setForeground(Color.RED);
|
||||
textField.setText("服务状态:关闭");
|
||||
//关闭任务
|
||||
System.exit(0);
|
||||
});
|
||||
|
||||
getContentPane().add(textField, BorderLayout.NORTH);
|
||||
|
||||
getContentPane().add(stopButton, BorderLayout.SOUTH);
|
||||
|
||||
}
|
||||
|
||||
public void setView(String view){
|
||||
getLogTextArea().append(view + "\n");
|
||||
}
|
||||
|
||||
public void initSocket() {
|
||||
if(SocketFactory.start()){
|
||||
setView("socket 服务启动成功");
|
||||
}else{
|
||||
setView("socket 服务启动失败,请检查8997端口是否被占用");
|
||||
}
|
||||
}
|
||||
|
||||
public void stopSocket() {
|
||||
try {
|
||||
if(SocketFactory.stop()){
|
||||
setView("服务停止成功");
|
||||
}else{
|
||||
setView("服务停止失败");
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
log.info("服务关闭");
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public JTextArea getLogTextArea(){
|
||||
if(logTextArea == null){
|
||||
initLogTextArea();
|
||||
}
|
||||
return logTextArea;
|
||||
}
|
||||
|
||||
public void initLogTextArea(){
|
||||
// 在这里添加你的组件初始化代码
|
||||
// 创建一个JTextArea用于显示日志
|
||||
logTextArea = new JTextArea();
|
||||
logTextArea.setEditable(false); // 设置为不可编辑
|
||||
logTextArea.setEnabled(false);
|
||||
|
||||
// 将JTextArea放入JScrollPane中以支持滚动
|
||||
JScrollPane scrollPane = new JScrollPane(logTextArea);
|
||||
|
||||
// 将JScrollPane添加到窗口中
|
||||
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> new NewWindow().setVisible(true));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue