java窗体课程_Java---JFrame窗口实现新课程添加

这个Java程序创建了一个窗体用于新课程信息录入,包括课程名称、任课教师和上课地点。课程名称必须唯一,教师信息限定在5位教师内,上课地点需从4个指定地点选择。信息验证通过后保存到txt文档。程序中利用JFrame、JPanel等组件构建界面,使用BufferedReader和BufferedWriter进行文件操作。
摘要由CSDN通过智能技术生成

任务要求:(1)新课程信息必须唯一,如有重复,提示课程名称重复,重新录入;(2)要求判断任课教师为5位教师中的一位(此处不写明教师名称);(3)要求上课地点开头为”一教,二教,三教,基教“四个地点中的一个;(4)将信息保存入库,此处老师要求将信息存入txt文档即可

代码如下:

1 importjava.awt.Font;2 import java.awt.event.*;3 importjava.io.BufferedWriter;4 importjava.io.BufferedReader;5 importjava.io.File;6 importjava.io.FileReader;7 importjava.io.FileWriter;8 import javax.swing.*;9

10 class screen extendsJFrame{11 private JFrame js = new JFrame("提示");12 private JPanel jsp = newJPanel();13 private JLabel error1l = new JLabel("课程名称重复,请重新录入");14 private JLabel error2l = new JLabel("教师信息出错");15 private JLabel error3l = new JLabel("上课地点错误");16 private JLabel finishl = new JLabel("操作成功");17

18 public screen() { //生成窗体

19 js.setSize(400, 150);20 js.setLocationRelativeTo(null);21 js.add(jsp);22 js.setDefaultCloseOperation(2);23 js.setVisible(true);24 }25

26 public void err1() { //提示错误1:课程名称错误

27 error1l.setFont(new Font("Dialog",1,20));28 jsp.add(error1l);29 }30

31 public void err2() { //提示错误2:教师信息错误

32 error2l.setFont(new Font("Dialog",1,20));33 jsp.add(error2l);34 }35

36 public void err3() { //提示错误3:上课地点错误

37 error3l.setFont(new Font("Dialog",1,20));38 jsp.add(error3l);39 }40 public void fin() { //提示成功

41 finishl.setFont(new Font("Dialog",1,20));42 jsp.add(finishl);43 }44

45 }46 public class NewClassIn extendsJFrame{47 private JFrame jf = new JFrame("LOGIN");48 private JPanel jp = newJPanel();49 private JLabel classL = new JLabel("课程名称:");50 private static JTextField classT = newJTextField();51 private JLabel teacherL = new JLabel("任课教师:");52 private static JTextField teacherT = newJTextField();53 private JLabel locationL = new JLabel("上课地点:");54 private static JTextField locationT = newJTextField();55 private JButton loginB = new JButton("保存");56

57 File file = new File("manage.txt"); //存入所有信息的文档

58 File file2 = new File("class.txt"); //存入课程信息的文档,用于判定课程是否重复

59 String jud1[] = {"王建民","刘立嘉","刘丹","王辉","杨子光"};60 String jud2[] = {"一教","二教","三教","基教"};61

62 publicNewClassIn() {63 jf.setSize(450, 300);64 jf.setLocationRelativeTo(null);65 jf.add(jp);66 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);67 Components(jp);68 jf.setVisible(true);69 loginB.addActionListener(new ActionListener() { //监听事件

70 public voidactionPerformed(ActionEvent c) {71 String s1 =classT.getText();72 String s2 =teacherT.getText();73 String s3 =locationT.getText();74 int temp = 1; //配合if语句进行检查操作。

75 if(temp == 1) //检查课程名称

76 {77 try{78 FileReader fr = newFileReader(file2);79 BufferedReader bufr = newBufferedReader(fr);80 String s0 = null;81 while((s0 = bufr.readLine())!=null) {82 if(s1.equals(s0))83 {84 screen k1 = newscreen();85 k1.err1();86 temp--;87 break;88 }89 }90 bufr.close();91 fr.close();92 }catch(Exception e) {93 e.printStackTrace();94 }95 temp++;96 }97 if(temp == 2) //检查教师信息

98 {99 int temp2 = 0;100 for(int i = 0;i < jud1.length;i++)101 {102 if(s2.equals(jud1[i]))103 {104 temp2 = 1;105 break;106 }107 }108 if(temp2 == 0)109 {110 temp--;111 screen k2 = newscreen();112 k2.err2();113 }114 else

115 {116 temp++;117 }118 }119 if(temp == 3) //检查上课地点

120 {121 int temp3 = 0;122 String subs3 = s3.substring(0, 2);123 for(int i = 0; i < jud2.length; i++)124 {125 if(subs3.equals(jud2[i]))126 {127 temp3 = 1;128 break;129 }130 }131 if(temp3 == 0)132 {133 temp--;134 screen k3 = newscreen();135 k3.err3();136 }137 else

138 {139 temp++;140 }141 }142 if(temp == 4) //确认无误,存入文档

143 {144 try{145 FileWriter out = new FileWriter(file,true);146 FileWriter out2 = new FileWriter(file2,true);147 BufferedWriter bufw = newBufferedWriter(out);148 BufferedWriter bufw2 = newBufferedWriter(out2);149 String str1 =classT.getText();150 String str2 =teacherT.getText();151 String str3 =locationT.getText();152 bufw.write(str1);153 bufw.newLine();154 bufw2.write(str1);155 bufw2.newLine();156 bufw.write(str2);157 bufw.newLine();158 bufw.write(str3);159 bufw.newLine();160 bufw.close();161 bufw2.close();162 out.close();163 out2.close();164 }catch(Exception e) {165 e.printStackTrace();166 }167 screen k4 = new screen(); //弹出操作成功提示框

168 k4.fin();169 }170 }171 });172 }173

174 private void Components(JPanel p) { //向窗体添加标签

175 p.setLayout(null);176 classL.setBounds(60, 30, 100, 25);177 classL.setFont(new Font("Dialog",1,20));178 p.add(classL);179 classT.setBounds(160, 30, 165, 25);180 p.add(classT);181 teacherL.setBounds(60, 60, 100, 25);182 teacherL.setFont(new Font("Dialog",1,20));183 p.add(teacherL);184 teacherT.setBounds(160, 60, 165, 25);185 p.add(teacherT);186 locationL.setBounds(60, 90, 100, 25);187 locationL.setFont(new Font("Dialog",1,20));188 p.add(locationL);189 locationT.setBounds(160, 90, 165, 25);190 p.add(locationT);191 loginB.setBounds(150, 150, 80, 25);192 p.add(loginB);193 }194 public static void main(String[] args) { //主函数,执行操作

195 newNewClassIn();196 }197 }

运行结果显示:

e20bcf5fa55a383c43c80cdd647e9aae.png

d84a61213602ce6511e5f09873bcdf48.png

8ce412d7cf259620aba38143f29b563c.png

2941ed1cd6c262c32254f32e4cf74442.png

5ab5803581371caf5c060f4f59341bff.png

因未知原因,原JFrame窗口中的字体大小与主窗体(LOGIN)中按钮的“保存”字体一样大小,因此使用setFont调整了字体大小。完成该任务核心在于窗体构建,信息录入以及核对信息。抓取输入文本框的信息运用getText()函数。核对时,教师信息和上课地点要求有明确规划,直接用字符串数组存入,届时遍历数组比较即可。而课程信息存储数量未知,因此除了用于存储全部信息的“manage.txt”文件外,另外设置了单独存储课程名称信息的“class.txt”文件,在核对课程名称信息时,只需读取该文件并进行比较即可。

另外,为了方便文件的读取操作,这里使用的是BufferedWriter和BufferedReader,目的是将信息单行录入,读取时只要单行读取即可。

最后就是窗体构建,着重使用JFrame,JPanel,JLabel和JButton,其窗体大小和组件大小数值可自行调试,代码中的数值是多次调试之后确定的个人比较满意的数值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值