其他分享
首页 > 其他分享> > 课堂派监听器作业

课堂派监听器作业

作者:互联网

第一题

package com.company;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    public static void main(String[] args) {
        JFrame jf=new JFrame("判断我点了哪个按钮");
        jf.setSize(500,400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setLayout(null);
        JButton jb1=new JButton("是");
        JButton jb2=new JButton("否");
        JButton jb3=new JButton("取消");
        jb1.setBounds(0,0,60,50);
        jb2.setBounds(70,0,60,50);
        jb3.setBounds(140,0,60,50);
        jf.add(jb1);
        jf.add(jb2);
        jf.add(jb3);
        JLabel jl=new JLabel("这里显示您单击按钮的信息");
        jl.setBounds(210,0,200,50);
        jf.add(jl);
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jl.setText("您单击了按钮‘是’");
            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jl.setText("您单击了按钮‘否’");
            }
        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jl.setText("您单击了按钮‘取消’");
            }
        });
	// write your code here
    }
}

在这里插入图片描述
第二题

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
        JFrame jf=new JFrame("User Login");
        jf.setSize(500,400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setLayout(null);
        JLabel jl1=new JLabel("用户名:");
        JLabel jl2=new JLabel("密码:");
        jl1.setBounds(150,0,50,50);
        jl2.setBounds(160,50,100,50);
        jf.add(jl1);
        jf.add(jl2);
        JButton jb1=new JButton("登录");
        JButton jb2=new JButton("重置");
        jb1.setBounds(335,15,70,20);
        jb2.setBounds(335,65,70,20);
        jf.add(jb1);
        jf.add(jb2);
        JTextField text=new JTextField();
        JPasswordField password=new JPasswordField();
        text.setBounds(200,15,115,20);
        password.setBounds(200,65,115,20);
        jf.add(text);
        jf.add(password);
        JLabel jl=new JLabel("提示信息:登录系统!");
        jl.setBounds(200,100,200,20);
        jf.add(jl);
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int flag=0;
                String Text=text.getText();
                String Password= String.valueOf(password.getPassword());
               Connection con=null;
               try{
                   con=sqlConnection.getConnection();
                   String sql="select * from students";
                   PreparedStatement statement=con.prepareStatement(sql);
                   ResultSet rs=statement.executeQuery();
                   while (rs.next()){
                       String sno=rs.getString("sno");
                       String pwd=rs.getString("pwd");
                       if(sno.equals(Text)&&pwd.equals(Password)){
                           flag=1;
                       }
                   }
                   if(flag==1){
                       jl.setText("提示信息:登陆成功!");
                   }
                   else{
                       jl.setText("提示信息:登陆失败!");
                   }
               }
               catch (Exception e21) {
                   e21.printStackTrace();
                   JOptionPane.showMessageDialog(null, "登录异常");
               } finally {
                   try {
                       sqlConnection.closeCon(con);
                   } catch (Exception e31) {
                       e31.printStackTrace();
                   }
               }

            }
        });
       jb2.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               text.setText("");
               password.setText("");
           }
       });

    }

}
 class sqlConnection {
    public static Connection getConnection()throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","123");
        return con;
    }
    public static void closeCon(Connection con)throws Exception{
        if(con!=null){
            con.close();
        }
    }
}

在这里插入图片描述
在这里插入图片描述

我的数据库
在这里插入图片描述

标签:作业,public,jl,add,监听器,import,new,课堂,jf
来源: https://blog.csdn.net/qq_52746199/article/details/117789576