Springboot整合Swing制作简单GUI客户端项目记录

业务的要求千奇百怪,今天要写个GUI客户端,JAVA是无所不能的

Swing 和 JavaFx

以前学java的时候,用过一点Swing,而JavaFx没有接触过,所以没选。

若两者都没用过,强烈建议使用JavaFx,Swing已经停止更新维护,样式风格像上古的windows 98,JavaFx是08年Oracle推出的新项目,界面趋势基本是Web UI了,是一个新时代。

我使用了美化ui来规避Swing极其丑陋的外观

Springboot项目整合Swing

新建一个Springboot web项目,用来支持后续数据库操作,暴露接口等服务。

新建样式类并继承JFrame

public class SwingArea extends JFrame {
    private static SwingArea instance = null;
    private JProgressBar progressBar;
    private SwingArea() {
    }
    public static SwingArea getInstance() {
        if (null == instance) {
            synchronized (SwingArea.class) {
                if (null == instance) {
                    instance = new SwingArea();
                }
            }
        }
        return instance;
    }
    public void initUI() {
    }

springboot启动类中启动GUI

public AdcDaApplication() {
      SwingArea.getInstance().initUI();
  }
public static void main(String[] args) {
    ApplicationContext ctx = new SpringApplicationBuilder(AdcDaApplication.class)
            .headless(false).run(args);
}

此时启动项目,就会执行initUI()方法来GUI窗口。下面我们将编写具体样式

Swing 使用 beautyeye_lnf.jar 美化

Swing官方样式极丑无比,为了复合目前主流审美,所以使用了美化插件: beautyeye_lnf美化插件

  1. 下载插件jar到项目目录

  2. maven引入本地beautyeye_lnf.jar


  beautyeye_lnf
  beautyeye_lnf
  3.7
  system
  ${project.basedir}/lib/beautyeye_lnf.jar

  1. 配置mavne打包时包含本地jar
 
    org.springframework.boot
    spring-boot-maven-plugin
  
    true
  

  1. 启动类中是样式生效

具体样式,可以翻阅官方文档

 public static void main(String[] args) {
    try {
        org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.translucencyAppleLike;
        UIManager.put("RootPane.setupButtonVisible", false);
    } catch(Exception e) {
        //TODO exception
    }
    ApplicationContext ctx = new SpringApplicationBuilder(AdcDaApplication.class)
            .headless(false).run(args);
}

官方美化示例:
[站外图片上传中...(image-99d016-1591260093260)]

具体样式及按钮核心配置在 initUI()中

JFrame窗口配置

this.setTitle("商用车CODE数据处理程序");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 700, 540);
  • setTitle 窗口顶端显示标题
  • setResizable 设置窗口大小不可调整
  • setDefaultCloseOperation 设置为关闭窗口同事关闭项目
  • setBounds 设置窗口位置大小 (x,y,w,h)

JPanel面板设置

JPanel panel = new JPanel();
panel.setLayout(null);
this.setContentPane(panel);

setLayout 清空默认浮动样式,来使后面定位数据生效

启动图


load

选择文件按钮设置

JButton openBtn = new JButton("选择文件");
openBtn.addActionListener(e -> file.set(showFileOpenDialog(this, fileFild)));
openBtn.setBounds(160,100,100,30);
openBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
openBtn.setFont(new Font("宋体", Font.BOLD,15));
openBtn.setForeground(Color.white);//字体颜色
panel.add(openBtn);
  • new JButton("选择文件") 按钮显示文本
  • addActionListener 点击事件执行文件选择
  • setUI 绿色背景
  • setFont 字体较粗
  • setForeground 字体颜色

swing文件选择

private JFileChooser showFileOpenDialog(Component parent, JLabel textField) {

    if (progressBar.getValue() != 0 && progressBar.getValue() != 100) {
        JOptionPane.showMessageDialog(null, "计算过程中,不可更改文件!╮(╯▽╰)╭", "警告!", JOptionPane.WARNING_MESSAGE);
        return null;
    }
    JFileChooser jFileChooser = new JFileChooser();
    jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //只能选择文件
    jFileChooser.setMultiSelectionEnabled(false); //只能选择一个文字
    // 设置默认使用的文件过滤器
    jFileChooser.setFileFilter(new FileNameExtensionFilter("excel(*.xlsx, *.xls)", "xls", "xlsx"));
    // 打开文件选择框(线程将被阻塞, 直到选择框被关闭)
    int result = jFileChooser.showOpenDialog(parent);
    if (result == JFileChooser.APPROVE_OPTION) {
        // 如果点击了"确定", 则获取选择的文件路径
        File file = jFileChooser.getSelectedFile();
        // 如果允许选择多个文件, 则通过下面方法获取选择的所有文件
        // File[] files = fileChooser.getSelectedFiles();
        textField.setText("");
        textField.setText(file.getName() + "\n\n");
    }
    //进度条归零
    progressBar.setValue(0);
    return jFileChooser;
}

选择文件
[站外图片上传中...(image-9b58e2-1591260093260)]

swing提醒弹窗

当异常操作时,系统应弹窗阻止操作

if (progressBar.getValue() != 0 && progressBar.getValue() != 100) {
    JOptionPane.showMessageDialog(null, "计算过程中,不可更改文件!╮(╯▽╰)╭", "警告!", JOptionPane.WARNING_MESSAGE);
    return null;
}

错误提示


error1

error2

点击开始执行数据处理

选择文件路径后,对文件进行处理,并显示处理百分百

按钮同上,执行操作代码为:

private void action(JFileChooser fileChooser) {
    if (null == fileChooser || null == fileChooser.getSelectedFile()) {
        JOptionPane.showMessageDialog(null, "请先选择要处理的文件!╮(╯▽╰)╭", "警告!",JOptionPane.WARNING_MESSAGE);
        return;
    }
    System.out.println("执行" + fileChooser.getSelectedFile().getAbsolutePath());
    progressBar(progressBar);

}

progressBar进度模拟程序

给窗口添加进度条:

private JProgressBar progressBar;

progressBar = new JProgressBar();
progressBar.setBounds(80,300,500,30);
progressBar.setValue(0);
progressBar.setStringPainted(true);

panel.add(progressBar);

模拟百分百:

 /**
  * 进度条模拟程序
  * @param progressBar
  */
private void progressBar(JProgressBar progressBar) {
    new Thread(() -> {
        for (int i = 0; i <= 100; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            progressBar.setValue(i);
        }
    }).start();
}

处理完成


finish

完整程序代码

/**
 * Created by Youdmeng on 2020/6/4 0004.
 */
public class SwingArea extends JFrame {

    private static SwingArea instance = null;
    private JProgressBar progressBar;
    private SwingArea() {
    }
    public static SwingArea getInstance() {
        if (null == instance) {
            synchronized (SwingArea.class) {
                if (null == instance) {
                    instance = new SwingArea();
                }
            }
        }
        return instance;
    }
    public void initUI() {
        this.setTitle("商用车CODE数据处理程序");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 700, 540);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        this.setContentPane(panel);
        //文本区域
        JLabel fileFild = new JLabel("无");

        AtomicReference file = new AtomicReference<>(new JFileChooser());

        JButton openBtn = new JButton("选择文件");
        openBtn.addActionListener(e -> file.set(showFileOpenDialog(this, fileFild)));
        openBtn.setBounds(160,100,100,30);
        openBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        openBtn.setFont(new Font("宋体", Font.BOLD,15));
        openBtn.setForeground(Color.white);//字体颜色
        panel.add(openBtn);

        JButton action = new JButton("执行计算");
        action.setBounds(370,100,100,30);
        action.addActionListener(e -> action(file.get()));
        action.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        action.setFont(new Font("宋体", Font.BOLD,15));
        action.setForeground(Color.white);
        panel.add(action);
        panel.add(fileFild);

        JLabel fileFildTitle = new JLabel("已选文件:");
        fileFildTitle.setBounds(130, 150, 150, 30);
        panel.add(fileFildTitle);
        fileFild.setBounds(200,150,500,30);
        progressBar = new JProgressBar();
        progressBar.setBounds(80,300,500,30);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

        panel.add(progressBar);
        this.setVisible(true);

        this.setVisible(true);

    }

    /**
     * 进度条模拟程序
     * @param progressBar
     */
    private void progressBar(JProgressBar progressBar) {
        new Thread(() -> {
            for (int i = 0; i <= 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                progressBar.setValue(i);
            }
        }).start();
    }

    private void action(JFileChooser fileChooser) {
        if (null == fileChooser || null == fileChooser.getSelectedFile()) {
            JOptionPane.showMessageDialog(null, "请先选择要处理的文件!╮(╯▽╰)╭", "警告!",JOptionPane.WARNING_MESSAGE);
            return;
        }
        System.out.println("执行" + fileChooser.getSelectedFile().getAbsolutePath());
        progressBar(progressBar);

    }

    /*
     * 打开文件
     */
    private JFileChooser showFileOpenDialog(Component parent, JLabel textField) {

        if (progressBar.getValue() != 0 && progressBar.getValue() != 100) {
            JOptionPane.showMessageDialog(null, "计算过程中,不可更改文件!╮(╯▽╰)╭", "警告!", JOptionPane.WARNING_MESSAGE);
            return null;
        }

        JFileChooser jFileChooser = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jFileChooser.setMultiSelectionEnabled(false);
        // 设置默认使用的文件过滤器
        jFileChooser.setFileFilter(new FileNameExtensionFilter("excel(*.xlsx, *.xls)", "xls", "xlsx"));
        // 打开文件选择框(线程将被阻塞, 直到选择框被关闭)
        int result = jFileChooser.showOpenDialog(parent);

        if (result == JFileChooser.APPROVE_OPTION) {
            // 如果点击了"确定", 则获取选择的文件路径
            File file = jFileChooser.getSelectedFile();

            // 如果允许选择多个文件, 则通过下面方法获取选择的所有文件
            // File[] files = fileChooser.getSelectedFiles();
            textField.setText("");
            textField.setText(file.getName() + "\n\n");
        }
        //进度条归零
        progressBar.setValue(0);
        return jFileChooser;
    }
}

收工





更多好玩好看的内容,欢迎到我的博客交流,共同进步        WaterMin


你可能感兴趣的:(Springboot整合Swing制作简单GUI客户端项目记录)