一、题目
二、代码
package text20191014; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import javax.swing.*; import java.math.*; public class NewGUI extends JFrame implements ActionListener{ private JLabel name,pass,yzm,code,jpg; private JTextField tf1,tf3,tf4,tf2; private JPasswordField tf5; private Button login,register1,register2,jp1; private JPanel back,jp2; private JFrame j1,j2; private Icon icon; private String[] kecheng=new String[100]; private String[] laoshi=new String[100]; private String[] didian=new String[100]; private int kesum=0,lssum=0,ddsum=0; static String str,user,pwd; public static void main(String[] args) { NewGUI ng = new NewGUI(); ng.ShowFrame(); } public void ShowFrame() { j1 = new JFrame(); j1.setTitle("课程录入"); //j.setLayout(new FlowLayout()); j1.setLayout(null); //自定义布局 //设置账号及其输入框 name = new JLabel("课程名称:"); Font font = new Font("楷体", Font.PLAIN, 15); tf1 = new JTextField("课程名称",20); tf1.setFont(font); tf1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { if(tf1.getText().equals("课程名称")) tf1.setText(""); } } }); name.setBounds(50, 13, 60, 40); tf1.setBounds(120, 20, 210, 25); tf1.addActionListener(this); //设置密码及其输入框 pass = new JLabel("任课教师:"); tf2 = new JTextField(20); pass.setBounds(50, 53, 60, 40); tf2.setBounds(120, 60, 210, 25); //设置验证码输入框并添加到面板三 yzm = new JLabel("上课地点:"); tf3 = new JTextField(20); yzm.setBounds(50, 93, 60, 40); tf3.setBounds(120, 100, 210, 25); //设置面板来存验证码 // paint(); //设置登陆按钮 login = new Button("录入"); login.setBackground(Color.orange); login.setBounds(80, 150, 240, 40); login.addActionListener(this); j1.setVisible(true); j1.setSize(400, 300); j1.setLocationRelativeTo(null); j1.setResizable(false); j1.setDefaultCloseOperation(3); j1.add(name); j1.add(tf1); j1.add(pass); j1.add(tf2); j1.add(yzm); j1.add(tf3); j1.add(login); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="录入") { int i = 0; boolean pd1=true,pd2=true,pd3=true; //课程检查 for(i = 0;i<kesum;i++) { if(kecheng[i].equals(tf1.getText())) { JOptionPane.showMessageDialog(null,"课程名称重复,重新输入!"); pd1=false; } } //老师的检查 if(tf2.getText().equals("王建民")||tf2.getText().equals("刘立嘉")||tf2.getText().equals("刘丹")||tf2.getText().equals("王辉")||tf2.getText().equals("杨子光")) { pd2=true; } else { JOptionPane.showMessageDialog(null,"请输入正确的老师姓名!"); pd2=false; } //地点检查 if(tf3.getText().charAt(1)=='教') { if(tf3.getText().charAt(0)=='一'||tf3.getText().charAt(0)=='二'||tf3.getText().charAt(0)=='三'||tf3.getText().charAt(0)=='基') { pd3=true; } else { pd3=false; JOptionPane.showMessageDialog(null,"请输入正确的上课地点!"); } } else { pd3=false; JOptionPane.showMessageDialog(null,"请输入正确的上课地点!"); } if(pd1&&pd2&&pd3) { kecheng[kesum++]=tf1.getText(); laoshi[lssum++]=tf2.getText(); didian[ddsum++]=tf3.getText(); JOptionPane.showMessageDialog(null,"录入成功!"); //创建源 File file = new File("dat1.txt"); //选择流 Writer writer = null; try { writer = new FileWriter(file,true); //操作流 writer.append(kecheng[kesum-1]).append("---").append(laoshi[lssum-1]).append("---").append(didian[ddsum-1]).append("\r\n"); writer.flush(); } catch (FileNotFoundException e1) { e1.printStackTrace(); }catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }finally { try { if(writer!=null) writer.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } } }
三、运行结果
1.主界面
2.录入成功
3.课程重复
4.教师姓名不正确
5.导出的信息