您好,登錄后才能下訂單哦!
基本思路:
先創(chuàng)建出一個添加課程信息的框架,隨后就設(shè)置按鈕的鼠標(biāo)監(jiān)聽事件,確保單機后錄入信息的合法性,以及確定合法性之后的后續(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("上課地點"); 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í)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。