基本思路:

先创建出一个添加课程信息的框架,随后就设置按钮的鼠标监听事件,确保单机后录入信息的合法性,以及确定合法性之后的后续操作,如保存课程信息,信息有误弹出窗口等操作。

代码

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)    //设置面板
  {
    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() {  //增加事件监听器

      @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窗口设置窗口参数
    // 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            //创建弹出窗口
{
  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);

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。