1 import java.awt.Color; 2 import java.awt.Container; 3 import java.awt.FlowLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 9 import javax.swing.*; 10 11 public class Test extends JFrame{ 12 int i=0; 13 String arr[][]=new String[30][3]; 14 15 JLabel la=new JLabel("课程名称"); 16 JLabel lb=new JLabel("任课教师"); 17 JLabel lc=new JLabel("上课地点"); 18 JTextField jt1=new JTextField(15); 19 JTextField jt2=new JTextField(15); 20 JTextField jt3=new JTextField(15); 21 22 JButton jb1=new JButton("录入"); 23 JButton jb2=new JButton("退出"); 24 25 public Test() { 26 setDefaultCloseOperation(EXIT_ON_CLOSE); 27 setTitle("课程信息录入"); 28 setBounds(580, 250, 320, 250); 29 setResizable(false); 30 31 Container c=getContentPane(); 32 c.add(la); 33 c.add(lb); 34 c.add(lc); 35 c.add(jt1); 36 c.add(jt2); 37 c.add(jt3); 38 c.add(jb1); 39 c.add(jb2); 40 41 la.setBounds(20,40,50,18); 42 lb.setBounds(20,80,50,18); 43 lc.setBounds(20, 120, 50, 18); 44 jt1.setBounds(70,40,200,18); 45 jt2.setBounds(70,80,200,18); 46 jt3.setBounds(70,120,200,18); 47 jb1.setBounds(40, 160, 80, 20); 48 jb2.setBounds(170, 160, 80, 20); 49 c.setLayout(null); 50 c .setBackground(Color.white); 51 setVisible(true); 52 } 53 void information() { 54 jb1.addActionListener(new ActionListener() {//录入按钮 55 public void actionPerformed(ActionEvent e) { 56 boolean flag1=true; 57 boolean flag2=true; 58 boolean flag3=true; 59 for(int j=0;j<i;j++) { 60 if(jt1.getText().equals(arr[j][0])) {//判断课程唯一 61 flag1=false; 62 break; 63 } 64 //判断老师姓名 65 if(!jt2.getText().equals("王建民")&&!jt2.getText().equals("刘立嘉")&&!jt2.getText().equals("刘丹")&&!jt2.getText().equals("王辉")&&!jt2.getText().equals("杨子光")) 66 { 67 flag2=false; 68 break; 69 } 70 //判断上课地点 71 if(jt3.getText().charAt(0)!='一'&&jt3.getText().charAt(0)!='二'&&jt3.getText().charAt(0)!='三'&&jt3.getText().charAt(0)!='基') { 72 flag3=false; 73 break; 74 } 75 if(jt3.getText().charAt(1)!='教') { 76 flag3=false; 77 break; 78 } 79 80 } 81 if(flag1&&flag2&&flag3) { 82 arr[i][0]=jt1.getText(); 83 arr[i][1]=jt2.getText(); 84 arr[i][2]=jt3.getText(); 85 i++; 86 JDialog jd=new JDialog(); 87 jd.setVisible(true); 88 jd.setBounds(650, 360, 100, 60); 89 Container c=jd.getContentPane(); 90 c.add(new JLabel("录入成功")); 91 } 92 else//弹出错误提示窗口 93 { 94 if(!flag1) { 95 JDialog jd=new JDialog(); 96 jd.setVisible(true); 97 jd.setBounds(650, 360, 100, 60); 98 Container c=jd.getContentPane(); 99 c.add(new JLabel("录入课程重复")); 100 } 101 if(!flag2) { 102 JDialog jd=new JDialog(); 103 jd.setVisible(true); 104 jd.setBounds(650, 360, 100, 60); 105 Container c=jd.getContentPane(); 106 c.add(new JLabel("录入教师姓名有误")); 107 } 108 if(!flag3) { 109 JDialog jd=new JDialog(); 110 jd.setVisible(true); 111 jd.setBounds(650, 360, 100, 60); 112 Container c=jd.getContentPane(); 113 c.add(new JLabel("录入上课地点有误")); 114 } 115 } 116 jt1.setText(""); 117 jt2.setText(""); 118 jt3.setText(""); 119 } 120 }); 121 jb2.addActionListener(new ActionListener() { 122 public void actionPerformed(ActionEvent e) { 123 fileExport(); 124 } 125 }); 126 } 127 void fileExport() { 128 try { 129 FileWriter fw=new FileWriter("temo",true);//表示不覆盖录入信息 130 for(int j=0;j<i;j++) { 131 String s=String.valueOf("课程名称:"+arr[j][0]+" 任课教师:"+arr[j][1]+" 上课地点:"+arr[j][2]); 132 fw.write(s+" "); 133 } 134 fw.close(); 135 } catch (IOException e) { 136 e.printStackTrace(); 137 } 138 } 139 public static void main(String[] args) { 140 Test test=new Test(); 141 test.information(); 142 test.fileExport(); 143 } 144 }
运行测试结果: