Java
第四题 import javax.swing.*; import java.awt.*; public class Login extends JFrame { // 定义组件 JLabel jl1, jl2; JTextField jtf; JPasswordField jpf; JButton jb; public Login() { // 创建组件 jl1 = new JLabel("用户名:"); jl2 = new JLabel("密码:"); jtf = new JTextField(10); jpf = new JPasswordField(10); jb = new JButton("登录"); // 设置布局为 GridLayout this.setLayout(new GridLayout(3, 2)); // 添加组件 this.add(jl1); this.add(jtf); this.add(jl2); this.add(jpf); this.add(jb); // 设置窗口属性 this.setTitle("用户登录"); this.setSize(300, 150); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); // 为登录按钮添加事件监听器 jb.addActionListener(e -> { // 获取用户名和密码 String username = jtf.getText(); char[] password = jpf.getPassword(); // 判断用户名和密码是否正确 if ("王恒".equals(username) && "202009280120".equals(new String(password))) { JOptionPane.showMessageDialog(this, "登录成功!"); } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } }); } public static void main(String[] args) { new Login(); } } 第五题 import java.io.*; public class Test5 { public static void main(String[] args) { File source = new File("d:\\test1.txt"); File target = new File("d:\\test2.txt"); if (!source.exists()) { return; } if (!target.getParentFile().exists()) { target.getParentFile().mkdirs(); } BufferedReader in = null; BufferedWriter out = null; try { in = new BufferedReader(new FileReader(source)); out = new BufferedWriter(new FileWriter(target)); String temp = null; while ((temp = in.readLine()) != null) { //输出到文件 out.write(temp + "\n"); } } catch (Exception e) { e.printStackTrace(); } finally { //关闭流 try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }