業(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美化插件
下載插件jar到項目目錄
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>
- 配置mavne打包時包含本地jar
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
- 啟動類中是樣式生效
具體樣式陌宿,可以翻閱官方文檔
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