Springboot整合Swing制作簡單GUI客戶端項目記錄

業(yè)務的要求千奇百怪,今天要寫個GUI客戶端魂莫,JAVA是無所不能的

Swing 和 JavaFx

以前學java的時候,用過一點Swing爹耗,而JavaFx沒有接觸過耙考,所以沒選。

若兩者都沒用過潭兽,強烈建議使用JavaFx倦始,Swing已經(jīng)停止更新維護,樣式風格像上古的windows 98山卦,JavaFx是08年Oracle推出的新項目鞋邑,界面趨勢基本是Web UI了,是一個新時代账蓉。

我使用了美化ui來規(guī)避Swing極其丑陋的外觀

Springboot項目整合Swing

新建一個Springboot web項目枚碗,用來支持后續(xù)數(shù)據(jù)庫操作,暴露接口等服務铸本。

新建樣式類并繼承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);
}

此時啟動項目肮雨,就會執(zhí)行initUI()方法來GUI窗口。下面我們將編寫具體樣式

Swing 使用 beautyeye_lnf.jar 美化

Swing官方樣式極丑無比箱玷,為了復合目前主流審美怨规,所以使用了美化插件: beautyeye_lnf美化插件

  1. 下載插件jar到項目目錄

  2. maven引入本地beautyeye_lnf.jar

<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>
  1. 配置mavne打包時包含本地jar
 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <includeSystemScope>true</includeSystemScope>
  </configuration>
</plugin>
  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數(shù)據(jù)處理程序");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 700, 540);
  • setTitle 窗口頂端顯示標題
  • setResizable 設置窗口大小不可調(diào)整
  • setDefaultCloseOperation 設置為關(guān)閉窗口同事關(guān)閉項目
  • setBounds 設置窗口位置大小 (x,y,w,h)

JPanel面板設置

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

setLayout 清空默認浮動樣式,來使后面定位數(shù)據(jù)生效

啟動圖


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 點擊事件執(zhí)行文件選擇
  • 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"));
    // 打開文件選擇框(線程將被阻塞, 直到選擇框被關(guān)閉)
    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提醒彈窗

當異常操作時呀舔,系統(tǒng)應彈窗阻止操作

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

錯誤提示


error1

error2

點擊開始執(zhí)行數(shù)據(jù)處理

選擇文件路徑后,對文件進行處理珠插,并顯示處理百分百

按鈕同上惧磺,執(zhí)行操作代碼為:

private void action(JFileChooser fileChooser) {
    if (null == fileChooser || null == fileChooser.getSelectedFile()) {
        JOptionPane.showMessageDialog(null, "請先選擇要處理的文件!╮(╯▽╰)╭", "警告捻撑!",JOptionPane.WARNING_MESSAGE);
        return;
    }
    System.out.println("執(zhí)行" + 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數(shù)據(jù)處理程序");
        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);
        //文本區(qū)域
        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("執(zhí)行計算");
        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("執(zhí)行" + 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"));
        // 打開文件選擇框(線程將被阻塞, 直到選擇框被關(guān)閉)
        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;
    }
}

收工





更多好玩好看的內(nèi)容江解,歡迎到我的博客交流设预,共同進步????????WaterMin


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市犁河,隨后出現(xiàn)的幾起案子鳖枕,更是在濱河造成了極大的恐慌,老刑警劉巖桨螺,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宾符,死亡現(xiàn)場離奇詭異,居然都是意外死亡灭翔,警方通過查閱死者的電腦和手機魏烫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缠局,“玉大人则奥,你說我怎么就攤上這事∠猎埃” “怎么了读处?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長唱矛。 經(jīng)常有香客問我罚舱,道長井辜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任管闷,我火速辦了婚禮粥脚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘包个。我一直安慰自己刷允,他們只是感情好,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布碧囊。 她就那樣靜靜地躺著树灶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪糯而。 梳的紋絲不亂的頭發(fā)上强缘,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天芍殖,我揣著相機與錄音搓蚪,去河邊找鬼劝赔。 笑死,一個胖子當著我的面吹牛瓜贾,可吹牛的內(nèi)容都是我干的诺祸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼阐虚,長吁一口氣:“原來是場噩夢啊……” “哼序臂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起实束,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤奥秆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后咸灿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體构订,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年避矢,在試婚紗的時候發(fā)現(xiàn)自己被綠了悼瘾。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡审胸,死狀恐怖亥宿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情砂沛,我是刑警寧澤烫扼,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站碍庵,受9級特大地震影響映企,放射性物質(zhì)發(fā)生泄漏悟狱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一堰氓、第九天 我趴在偏房一處隱蔽的房頂上張望挤渐。 院中可真熱鬧,春花似錦双絮、人聲如沸浴麻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽白胀。三九已至,卻和暖如春抚岗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背哪怔。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工宣蔚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人认境。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓胚委,卻偏偏與公主長得像,于是被迫代替她去往敵國和親叉信。 傳聞我的和親對象是個殘疾皇子亩冬,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容