没有收到回复的同学注意了,用它一键查询!
文章目錄
- JFrame實踐
- 打包
今天周六,起床打開Timi,輔導員就發來消息
媽呀,我的天!
雖是小工程,但想到要看著兩個白花花的名單一個個找…我的兩只眼睛就先痛為敬了。
哎?剛學了Java IO,我想能不能實現用程序幫我找出來呢?
這樣的瑣事以后肯定還有很多,廢話不多說,說干就干!!
于是乎,我答應了。
大腦飛速旋轉,打開idea寫了這個查找沒有收到回復的小demo,大致如下:
把信息都放到對應好的路徑后運行:
查找成功!!!
我趕忙告訴中隊,跟他炫耀一波我的小機靈!
誰想到他卻讓我發給他??
正當我剛要把代碼copy給他的時候,仔細一想,不對,不可以!
在這個提倡知識付費的時代,代碼不可以隨便給啊
這不就便宜他了?
JFrame實踐
(正文)
首先,創建一個主類,繼承JFrame
public class myApp extends JFrame {}在里面寫上它的構造方法(這是我們的主陣地)
public myApp(){}獲得一個JFrame窗體
JFrame c = new JFrame("Get the guys out !");設置窗體流布局
c.setLayout(new FlowLayout(FlowLayout.LEFT));設置窗體打開位置(前2個參數),窗體寬高(后2個參數)
c.setBounds(100, 100, 260, 200);// 下圖描述各個參數的含義添加2個提示標簽(如上圖)
JLabel bq1 = new JLabel("班級名單路徑 (例如:d:/名單.txt)"); JLabel bq2 = new JLabel("待查文本粘貼處");添加3個文本域JTextField(參數為框框長度)
JTextField text1 = new JTextField(20); JTextField text2 = new JTextField(20); JTextField text3 = new JTextField(20);
添加按鈕,并為其添加監聽事件(即按鈕按下時,觸發函數)
按順序添加所有的組件到窗口中
c.add(bq1);c.add(text1);c.add(bq2);c.add(text2);c.add(button);c.add(text3);設置窗體可見
c.setVisible(true);設置默認關閉方式(不設置可以關閉窗口,但進程不會關閉)
c.setDefaultCloseOperation(EXIT_ON_CLOSE);然后按照一開始的demo完成searchAbsentPerson函數
public static void searchAbsentPerson(JTextField text1, JTextField text2, JTextField text3) throws IOException {// 以緩存流的方式打開完整名單File file = new File(text1.getText());Reader input = new FileReader(file);BufferedReader in = new BufferedReader(input);StringBuilder ans = new StringBuilder("");String name = new String();// 按完整名單的順序讀取名字,并依次在待查文本中查找while ((name = in.readLine()) != null){// 用indexOf查找,返回值為-1,即此人缺席(沒有收到回復)if (text2.getText().indexOf(name) == -1) {ans.append(name + " ");}}// 在第三個文本域中打印查找結果text3.setText(ans.toString());}最后我們寫上啟動類
public static void main(String[] args) {new myApp();}大功告成!!看看效果!!
其實第一次Get的時候我亂碼了,把txt名單的編碼格式改成了ANSI就可以了
接下來就是打包的事情了 (完整代碼在文末,求好兄弟的一個三連!!)
打包
我用的是IDEA2020.3的版本,首先在File里找到Project Structure
在Artifacts里,點左上角的 + 號
JAR -> From modules with dependencies…,然后選擇主類(myApp)
然后,我們可以把名字改成我們喜歡的名字,導出路徑也可以修改
回到主界面,點導航欄的Build,再點里面的Build Artifacts
彈出框框,選擇Build即可
然后在設置好的路徑找到jar,雙擊就可以運行啦!
大搖大擺發給了輔導員
同學知道后
文末(完整源碼)
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*;public class myApp extends JFrame {public static void searchAbsentPerson(JTextField text1, JTextField text2, JTextField text3) throws IOException {// 以緩存流的方式打開完整名單File file = new File(text1.getText());Reader input = new FileReader(file);BufferedReader in = new BufferedReader(input);StringBuilder ans = new StringBuilder("");String name = new String();// 按完整名單的順序讀取名字,并依次在待查文本中查找while ((name = in.readLine()) != null){// 用indexOf查找,返回值為-1,即此人缺席(沒有收到回復)if (text2.getText().indexOf(name) == -1) {ans.append(name + " ");}}// 在第三個文本域中打印查找結果text3.setText(ans.toString());}public myApp(){JFrame c = new JFrame("Get the guys out !");c.setLayout(new FlowLayout(FlowLayout.LEFT));c.setBounds(100, 100, 260, 200);JLabel bq1 = new JLabel("班級名單路徑 (例如:d:/名單.txt)");JLabel bq2 = new JLabel("待查文本粘貼處");JTextField text1 = new JTextField(20);JTextField text2 = new JTextField(20);JTextField text3 = new JTextField(20);JButton button = new JButton("Get");button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {searchAbsentPerson(text1, text2, text3);} catch (IOException ioException) {ioException.printStackTrace();}}});c.add(bq1);c.add(text1);c.add(bq2);c.add(text2);c.add(button);c.add(text3);c.setVisible(true);c.setDefaultCloseOperation(EXIT_ON_CLOSE);}public static void main(String[] args) {new myApp();} }END
總結
以上是生活随笔為你收集整理的没有收到回复的同学注意了,用它一键查询!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gta5威斯普奇海滩位置在哪里
- 下一篇: 刺激战场不小心退出怎么观战