1.定义一个图形接口,一个有抽象方法。求周长子类实现接口,创建子类对象求周长
//1.定义一个图形接口,一个有抽象方法求周长 interface Shape{ double girth(int length); } //2.子类圆形实现接口 class Circle implements Shape{ int r; //创建有参构造方法 public Circle(int r) { super(); //将r赋值给成员变量r,this.r调用成员变量 this.r = r; } //重写girth()方法,求出周长 @Override public double girth(int length) { // TODO Auto-generated method stub return 2*3.14*length; } } //3.测试类 public class ShapeTest { public static void main(String[] args) { //将长度2添加到Circle对象中 Circle c = new Circle(2); //通过Circle对象调用girth()方法 System.out.println(c.girth(c.r)); } }
运行结果
12.56
2.随机输出500-560之间的30个,存到数组中,并升序排序
public class RandomTest { public static void main(String[] args) { //1.创建数组 int[] arr = new int[30]; //2.创建随机数对象 Random random = new Random(); //3.for循环,将随机数存到数组 for(int i = 0;i<arr.length;i++) { arr[i] = random.nextInt(61)+500; } //4.for-each全部输出,升序 Arrays.sort(arr); for (int a : arr) { System.out.print("\t"+a); } } }
运行结果
500 501 501 501 501 502 503 503 505 505 513 515 517 524 526 531 534 535 535 540 540 541 543 549 550 551 556 557 557 559
3.输出流输出Stu对象,序列化,输入流读取到控制台
public class ObjectTest { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { //创建有参对象 Stu s1 = new Stu("11","aa"); Stu s2 = new Stu("22","bb"); Stu s3 = new Stu("11","cc"); //创建集合,将对象添加进去 HashSet hs = new HashSet(); hs.add(s1); hs.add(s2); hs.add(s3); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\a.txt")); //迭代器遍历 Iterator iterator = hs.iterator(); while(iterator.hasNext()) { Stu stu = (Stu)iterator.next(); oos.writeObject(stu); } //关闭流 oos.close(); //对象输入流 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\a.txt")); //读数据 Stu stu1 = (Stu)ois.readObject(); Stu stu2 = (Stu)ois.readObject(); //显示在控制台 System.out.println(stu1); System.out.println(stu2); //关闭流 ois.close(); } } class Stu implements Serializable{ String id,name; public Stu() { super(); // TODO Auto-generated constructor stub } public Stu(String id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "id=" + id + ", name=" + name ; } @Override public int hashCode() { // TODO Auto-generated method stub return this.id.hashCode(); } @Override public boolean equals(Object obj) { Stu s = (Stu)obj; return this.id.equals(s.id); } }
运行结果
id=11, name=aa
id=22, name=bb
4.点击按钮判断登录是否成功
public class JframeLogin { public static void main(String[] args) { //创建窗口 JFrame frame = new JFrame("窗口"); //设置窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(500,200,300,200); //创建面板 JPanel panel = new JPanel(); //创建组件 Label label1 = new Label("账号:"); JTextField textField1 = new JTextField(10); Label label2 = new Label("密码:"); JPasswordField textField2 = new JPasswordField(10); JButton button = new JButton("登录"); //将组件添加到面板上 panel.add(label1); panel.add(textField1); panel.add(label2); panel.add(textField2); panel.add(button); //将面板添加到窗口上 frame.add(panel); //可见窗口 frame.setVisible(true); //事件处理 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //获取用户名输入框 String username = textField1.getText(); //获取密码框 String pwd = new String(textField2.getPassword()); //判断用户名和密码 if(username.equals("123") && pwd.equals("123")) { //弹窗 JOptionPane.showMessageDialog(frame, "登录成功"); }else { JOptionPane.showMessageDialog(frame, "登录失败"); } } }); } }
运行结果
5.在窗体上有标签,文本框,按钮,在文本框输入账号,点击确定,在数据库中查找是否存在
public class SelectTest { public static void main(String[] args) { // TODO Auto-generated method stub JFrame jf = new JFrame("查询"); jf.setBounds(500, 200, 400 ,300); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗体上放中间容器 JPanel jp = new JPanel(); //面板上摆放组件 JLabel jl = new JLabel("请输入账号"); JTextField field = new JTextField(15); JButton btn = new JButton("查找"); //将组件添加到面板上 jp.add(jl); jp.add(field); jp.add(btn); //将面板添加到窗体上 jf.add(jp); jf.setVisible(true); //事件处理 btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Connection con = null; Statement sta = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/teacherinfo?serverTimezone=UTC&useSSL=false"; String user = "root"; String password = "123456"; try { Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection(url, user, password); sta = con.createStatement(); rs = sta.executeQuery("select * from info"); boolean flag = false; while(rs.next()) { int id = rs.getInt(1); String idTxt = field.getText(); if (idTxt.equals(String.valueOf(id))) { flag = true; break; } } if (flag==true) { JOptionPane.showMessageDialog(jf, "查找到了"); }else { JOptionPane.showMessageDialog(jf, "没有找到"); } } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }finally { if (rs!=null) { try { rs.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } if (sta!=null) { try { sta.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } if(con!=null) { try { con.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); } }
运行结果