業務的要求千奇百怪,今天要寫個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