1.定义一个图形接口,一个有抽象方法。求周长子类实现接口,创建子类对象求周长
interface Shape{ double girth(int length); } class Circle implements Shape{ int r; public Circle(int r) { super(); this.r = r; } @Override public double girth(int length) { // TODO Auto-generated method stub return 2*3.14*length; } } public class ShapeTest { public static void main(String[] args) { Circle c = new Circle(1); System.out.println(c.girth(c.r)); } }
2.随机输出500-560之间的30个,存到数组中,并升序排序
public class RandomTest { public static void main(String[] args) { int[] array = new int[30]; Random random = new Random(); for(int i = 0;i<array.length;i++) { array[i] = random.nextInt(61)+500; } System.out.println("未排序"); for(int arr:array) { System.out.print(arr+"\t"); } Arrays.sort(array); System.out.println("\r\n排序"); for(int arr:array) { System.out.print(arr+"\t"); } } }
3.输出流输出Stu对象,序列化,输入流读取到控制台
public class ObjectTest { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Stu s1 = new Stu("1","a"); Stu s2 = new Stu("2","b"); Stu s3 = new Stu("1","a"); HashSet hs = new HashSet(); hs.add(s1); hs.add(s2); hs.add(s3); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\object.txt")); Iterator iterator = hs.iterator(); while(iterator.hasNext()) { Stu s = (Stu)iterator.next(); oos.writeObject(s); } oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\object.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); } }
4.点击按钮出现弹窗
public class JframeTest { public static void main(String[] args) { JFrame jf = new JFrame("标题"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setBounds(500, 200,400, 300); //窗体上放中间容器 JPanel pan = new JPanel(); //面板上摆放组件 JLabel label1 = new JLabel("账号"); JTextField text = new JTextField(10); // label1.setBounds(100, 10, 80, 70); JLabel label2 = new JLabel("密码"); // label2.setBounds(100, 10, 80,90); JButton button = new JButton("点击"); //将组件添加到面板上 pan.add(label1); pan.add(text); pan.add(label2); pan.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(jf, "这是消息框"); } }); //将面板添加到窗体上 jf.add(pan); //显示窗体 jf.setVisible(true); } }
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(); } } } } }); } }