基本思路:
先创建出一个添加课程信息的框架,随后就设置按钮的鼠标监听事件,确保单机后录入信息的合法性,以及确定合法性之后的后续操作,如保存课程信息,信息有误弹出窗口等操作。
1 package Test; 2 3 import javax.swing.JButton; 4 import java.io.*; 5 import javax.swing.JFrame; 6 import javax.swing.JLabel; 7 import javax.swing.JPanel; 8 import javax.swing.JTextField; 9 import java.awt.*; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 13 public class SetClass { 14 String str1="添加课程失败,请核对信息后添加", 15 str2="添加成功"; 16 JFrame jf=new JFrame("新课程添加"); 17 JPanel panel=new JPanel(); 18 JLabel addclass=new JLabel("课程名称"); 19 JTextField addclasstext=new JTextField(); 20 JLabel teacher=new JLabel("任课老师"); 21 JTextField teachertext=new JTextField(); 22 JLabel placeclass=new JLabel("上课地点"); 23 JTextField placeclasstext=new JTextField(); 24 JButton register=new JButton("添加"); 25 26 public boolean judgeText(String filepath,String s) //判断信息的合法性 27 { 28 String str=""; 29 boolean flag=false; 30 File file=new File(filepath); 31 try { 32 FileReader reader=new FileReader(file); 33 BufferedReader br=new BufferedReader(reader); 34 while((str=br.readLine())!=null) 35 { 36 if(str.equals(s)) 37 flag=true; 38 } 39 br.close(); 40 reader.close(); 41 } catch (FileNotFoundException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 49 return flag; 50 51 } 52 public void inputFile(String filepath,JTextField jt) //将信息录入到文件中 53 { 54 File file=new File(filepath); 55 try { 56 FileWriter reader = new FileWriter(file,true); 57 BufferedWriter bw=new BufferedWriter(reader); 58 bw.newLine(); 59 bw.write(jt.getText()); 60 bw.close(); 61 } catch (IOException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 public void setPanel(JPanel panel) //设置面板 67 { 68 panel.setLayout(null); 69 addclass.setBounds(30, 30, 80, 25); 70 teacher.setBounds(30, 60, 80, 25); 71 placeclass.setBounds(30, 90, 80, 25); 72 register.setBounds(110, 120, 80, 25); 73 register.addActionListener(new ActionListener() { //增加事件监听器 74 75 @Override 76 public void actionPerformed(ActionEvent arg0) { 77 // TODO Auto-generated method stub 78 if(!judgeText("data", addclasstext.getText())&&judgeText("data", teachertext.getText())&&judgeText("data", placeclasstext.getText().substring(0, 2))) 79 { 80 inputFile("data",addclasstext); 81 inputFile("Xclass",addclasstext); 82 inputFile("Xclass",teachertext); 83 inputFile("Xclass",placeclasstext); 84 new MyJf(str2); 85 } 86 else 87 new MyJf(str1); 88 } 89 }); 90 addclasstext.setBounds(105, 30, 165, 25); 91 teachertext.setBounds(105, 60, 165, 25); 92 placeclasstext.setBounds(105, 90, 165, 25); 93 panel.add(addclass); 94 panel.add(addclasstext); 95 panel.add(teacher); 96 panel.add(teachertext); 97 panel.add(register); 98 panel.add(placeclass); 99 panel.add(placeclasstext); 100 } 101 102 SetClass() { //为JFrame窗口设置窗口参数 103 // TODO Auto-generated constructor stub 104 jf.setSize(340, 250); 105 jf.setLocationRelativeTo(null); 106 jf.add(panel); 107 setPanel(panel); 108 jf.setVisible(true); 109 jf.setDefaultCloseOperation(3); 110 } 111 112 public static void main(String[] args) { 113 // TODO Auto-generated method stub 114 new SetClass(); 115 } 116 117 } 118 class MyJf //创建弹出窗口 119 { 120 JFrame jf1=new JFrame("提示信息"); 121 JPanel jp1=new JPanel(); 122 JLabel jl=new JLabel(); 123 MyJf(String str) 124 { 125 jl.setText(str); 126 jf1.setSize(300, 80); 127 jf1.setVisible(true); 128 jf1.add(jp1); 129 jf1.setDefaultCloseOperation(2); 130 jf1.setLocationRelativeTo(null); 131 jp1.add(jl); 132 133 } 134 }
原文地址:https://www.cnblogs.com/haheihei/p/11700856.html
时间: 2024-11-05 19:01:12