JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析
基本思路:
先創(chuàng)建出一個(gè)添加課程信息的框架,隨后就設(shè)置按鈕的鼠標(biāo)監(jiān)聽事件,確保單機(jī)后錄入信息的合法性,以及確定合法性之后的后續(xù)操作,如保存課程信息,信息有誤彈出窗口等操作。
代碼
package Test; import javax.swing.JButton; import java.io.*; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SetClass { String str1="添加課程失敗,請核對信息后添加", str2="添加成功"; JFrame jf=new JFrame("新課程添加"); JPanel panel=new JPanel(); JLabel addclass=new JLabel("課程名稱"); JTextField addclasstext=new JTextField(); JLabel teacher=new JLabel("任課老師"); JTextField teachertext=new JTextField(); JLabel placeclass=new JLabel("上課地點(diǎn)"); JTextField placeclasstext=new JTextField(); JButton register=new JButton("添加"); public boolean judgeText(String filepath,String s) //判斷信息的合法性 { String str=""; boolean flag=false; File file=new File(filepath); try { FileReader reader=new FileReader(file); BufferedReader br=new BufferedReader(reader); while((str=br.readLine())!=null) { if(str.equals(s)) flag=true; } br.close(); reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } public void inputFile(String filepath,JTextField jt) //將信息錄入到文件中 { File file=new File(filepath); try { FileWriter reader = new FileWriter(file,true); BufferedWriter bw=new BufferedWriter(reader); bw.newLine(); bw.write(jt.getText()); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void setPanel(JPanel panel) //設(shè)置面板 { panel.setLayout(null); addclass.setBounds(30, 30, 80, 25); teacher.setBounds(30, 60, 80, 25); placeclass.setBounds(30, 90, 80, 25); register.setBounds(110, 120, 80, 25); register.addActionListener(new ActionListener() { //增加事件監(jiān)聽器 @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(!judgeText("data", addclasstext.getText())&&judgeText("data", teachertext.getText())&&judgeText("data", placeclasstext.getText().substring(0, 2))) { inputFile("data",addclasstext); inputFile("Xclass",addclasstext); inputFile("Xclass",teachertext); inputFile("Xclass",placeclasstext); new MyJf(str2); } else new MyJf(str1); } }); addclasstext.setBounds(105, 30, 165, 25); teachertext.setBounds(105, 60, 165, 25); placeclasstext.setBounds(105, 90, 165, 25); panel.add(addclass); panel.add(addclasstext); panel.add(teacher); panel.add(teachertext); panel.add(register); panel.add(placeclass); panel.add(placeclasstext); } SetClass() { //為JFrame窗口設(shè)置窗口參數(shù) // TODO Auto-generated constructor stub jf.setSize(340, 250); jf.setLocationRelativeTo(null); jf.add(panel); setPanel(panel); jf.setVisible(true); jf.setDefaultCloseOperation(3); } public static void main(String[] args) { // TODO Auto-generated method stub new SetClass(); } } class MyJf //創(chuàng)建彈出窗口 { JFrame jf1=new JFrame("提示信息"); JPanel jp1=new JPanel(); JLabel jl=new JLabel(); MyJf(String str) { jl.setText(str); jf1.setSize(300, 80); jf1.setVisible(true); jf1.add(jp1); jf1.setDefaultCloseOperation(2); jf1.setLocationRelativeTo(null); jp1.add(jl); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java,android,MD5加密算法的實(shí)現(xiàn)代碼(16位,32位)
下面小編就為大家?guī)硪黄猨ava,android,MD5加密算法的實(shí)現(xiàn)代碼(16位,32位)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09JavaWeb實(shí)戰(zhàn)之用Servlet+JDBC實(shí)現(xiàn)用戶登錄與注冊
這篇文章主要介紹了JavaWeb實(shí)戰(zhàn)之用Servlet+JDBC實(shí)現(xiàn)用戶登錄與注冊,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很大的幫助,需要的朋友可以參考下2021-04-04Mybatis利用分頁插件PageHelper快速實(shí)現(xiàn)分頁查詢
如果你也在用MyBatis,建議嘗試該分頁插件,這一定是最方便使用的分頁插件,這篇文章主要給大家介紹了關(guān)于Mybatis利用分頁插件PageHelper快速實(shí)現(xiàn)分頁查詢的相關(guān)資料,PageHelper是一個(gè)Mybatis的分頁插件,負(fù)責(zé)將已經(jīng)寫好的sql語句,進(jìn)行分頁加工,需要的朋友可以參考下2021-08-08Apache?Commons?CLI構(gòu)建命令行應(yīng)用利器教程
這篇文章主要為大家介紹了構(gòu)建命令行應(yīng)用利器Apache?Commons?CLI的使用教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12