业务的要求千奇百怪,今天要写个GUI客户端,JAVA是无所不能的
之前学java的时候,用过一点Swing,而JavaFx没有接触过,因此没选。html
若二者都没用过,强烈建议使用JavaFx,Swing已经中止更新维护,样式风格像上古的windows 98,JavaFx是08年Oracle推出的新项目,界面趋势基本是Web UI了,是一个新时代。java
我使用了美化ui来规避Swing极其丑陋的外观web
新建一个Springboot web项目,用来支持后续数据库操做,暴露接口等服务。spring
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() { }
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美化插件windows
<dependency> <groupId>beautyeye_lnf</groupId> <artifactId>beautyeye_lnf</artifactId> <version>3.7</version> <scope>system</scope> <systemPath>${project.basedir}/lib/beautyeye_lnf.jar</systemPath> </dependency>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin>
具体样式,能够翻阅官方文档springboot
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); }
官方美化示例:less
具体样式及按钮核心配置在 initUI()中maven
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);
setLayout 清空默认浮动样式,来使后面定位数据生效spring-boot
启动图
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);
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; }
选择文件
当异常操做时,系统应弹窗阻止操做
if (progressBar.getValue() != 0 && progressBar.getValue() != 100) { JOptionPane.showMessageDialog(null, "计算过程当中,不可更改文件!╮(╯▽╰)╭", "警告!", JOptionPane.WARNING_MESSAGE); return null; }
错误提示
选择文件路径后,对文件进行处理,并显示处理百分百
按钮同上,执行操做代码为:
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 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(); }
处理完成
/** * 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<JFileChooser> 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