import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class database extends JFrame{
static DefaultListModel m1=new DefaultListModel();
DefaultListModel m2=new DefaultListModel();
DefaultListModel m3=new DefaultListModel();
DefaultListModel m4=new DefaultListModel();
JList list1=new JList(m1);
JList list2=new JList(m2);
JList list3=new JList(m3);
JList list4=new JList(m4);
JLabel warn=new JLabel("请选择一个项目!");
static int i=0;
JLabel tip=new JLabel("提交失败");
String url="jdbc:mysql://localhost:3306/test?user=root&password=root";
Connection conn=DriverManager.getConnection(url);
Statement stmt = conn.createStatement( );
public database() throws ClassNotFoundException, SQLException
{
super("数据库");
JLabel name=new JLabel("name");
JLabel birthday=new JLabel("birthday");
JLabel homeAddress=new JLabel("homeAddress");
JLabel classNumber=new JLabel("classNumber");
Class.forName("com.mysql.jdbc.Driver");
conn.setAutoCommit(false);
ResultSet rs = stmt.executeQuery("SELECT * FROM `test`.`student`");
for(i=0;rs.next();i++)
{
m1.add(i, rs.getString("name"));
m2.add(i,rs.getString("birthday"));
m3.add(i, rs.getString("homeAddress"));
m4.add(i,rs.getString("classNumber"));
}
list1.addListSelectionListener(new list());
list2.addListSelectionListener(new list());
list3.addListSelectionListener(new list());
list4.addListSelectionListener(new list());
setLayout(null);
setBounds(500, 300, 550, 500);
name.setBounds(40,20,80,30);
add(name);
list1.setBounds(40,50,110,200);
add(list1);
birthday.setBounds(150,20,80,30);
add(birthday);
list2.setBounds(150,50,110,200);
add(list2);
homeAddress.setBounds(260,20,80,30);
add(homeAddress);
list3.setBounds(260,50,110,200);
add(list3);
classNumber.setBounds(370,20,80,30);
add(classNumber);
list4.setBounds(370,50,110,200);
add(list4);
JButton change=new JButton("修改");
change.setBounds(70,270,80,30);
change.addActionListener(new Change());
add(change);
JButton delete=new JButton("删除");
delete.setBounds(180,270,80,30);
delete.addActionListener(new DELETE());
add(delete);
warn.setBounds(270,270,150,30);
warn.setVisible(false);
add(warn);
JButton addnew=new JButton("添加");
addnew.setBounds(100,340,80,30);
addnew.addActionListener(new ADD());
add(addnew);
tip.setBounds(220, 340, 120, 30);
tip.setVisible(false);
add(tip);
JButton submit=new JButton("提交");
submit.setBounds(300,340,80,30);
submit.addActionListener(new SUBMIT());
add(submit);
JButton help=new JButton("帮助");
help.setBounds(400,340,80,30);
help.addActionListener(new HELP());
add(help);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e) {System.exit(0);}});
setVisible(true);
}
public class list implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent arg0) {
Object source1=arg0.getSource();
tip.setVisible(false);
if(source1==list1)
{
warn.setVisible(false);
list1.setSelectionBackground(Color.GREEN);
list2.setSelectionBackground(null);
list3.setSelectionBackground(null);
list4.setSelectionBackground(null);
}
if(source1==list2)
{
warn.setVisible(false);
list2.setSelectionBackground(Color.orange);
list1.setSelectionBackground(null);
list3.setSelectionBackground(null);
list4.setSelectionBackground(null);
}
if(source1==list3)
{
warn.setVisible(false);
list3.setSelectionBackground(Color.cyan);
list2.setSelectionBackground(null);
list1.setSelectionBackground(null);
list4.setSelectionBackground(null);
}
if(source1==list4)
{
warn.setVisible(false);
list4.setSelectionBackground(Color.pink);
list2.setSelectionBackground(null);
list3.setSelectionBackground(null);
list1.setSelectionBackground(null);
}
}
}
public class Change implements ActionListener{
JTextField cha=new JTextField(15);
JLabel empty=new JLabel("内容不能为空");
public void actionPerformed(ActionEvent arg1) {
tip.setVisible(false);
if(list1.getSelectedIndex()==-1&&list2.getSelectedIndex()==-1
&&list3.getSelectedIndex()==-1&&list4.getSelectedIndex()==-1)
{
warn.setVisible(true);
}
else
{
final JFrame frame1=new JFrame("修改");
JButton sure=new JButton("确定");
empty.setVisible(false);
if(list1.getSelectionBackground()==Color.GREEN)
{
cha.setText((String)list1.getSelectedValue());
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg2) {
if(cha.getText().equals(""))
{
empty.setVisible(true);
}
else
{empty.setVisible(false);
int select=list1.getSelectedIndex();
m1.remove(select);
m1.add(select, cha.getText());
frame1.dispose();}
}}
);
}
if(list2.getSelectionBackground()==Color.orange)
{
cha.setText((String)list2.getSelectedValue());
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg2) {
if(cha.getText().equals(""))
{
empty.setVisible(true);
}
else
{empty.setVisible(false);
int select=list2.getSelectedIndex();
m2.remove(select);
m2.add(select, cha.getText());
frame1.dispose();}
}}
);
}
if(list3.getSelectionBackground()==Color.cyan)
{
cha.setText((String)list3.getSelectedValue());
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg2) {
if(cha.getText().equals(""))
{
empty.setVisible(true);
}
else
{empty.setVisible(false);
int select=list3.getSelectedIndex();
m3.remove(select);
m3.add(select, cha.getText());
frame1.dispose();}
}}
);
}
if(list4.getSelectionBackground()==Color.pink)
{
cha.setText((String)list4.getSelectedValue());
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg2) {
if(cha.getText().equals(""))
{
empty.setVisible(true);
}
else
{empty.setVisible(false);
int select=list4.getSelectedIndex();
m4.remove(select);
m4.add(select, cha.getText());
frame1.dispose();}
}}
);
}
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout());
jp1.add(cha);
jp1.add(sure);
jp1.add(empty);
frame1.getContentPane( ).add(jp1);
frame1.setSize(300,100);
frame1.setVisible(true);
}
}
}
public class DELETE implements ActionListener{
public void actionPerformed(ActionEvent arg1) {
tip.setVisible(false);
if(list1.getSelectedIndex()==-1&&list2.getSelectedIndex()==-1
&&list3.getSelectedIndex()==-1&&list4.getSelectedIndex()==-1)
{
warn.setVisible(true);
return;
}
if(list1.getSelectionBackground()==Color.GREEN)
{
int dele=list1.getSelectedIndex();
m1.remove(dele);m2.remove(dele);
m3.remove(dele);m4.remove(dele);
i--;
}
if(list2.getSelectionBackground()==Color.orange)
{
int dele=list2.getSelectedIndex();
m1.remove(dele);m2.remove(dele);
m3.remove(dele);m4.remove(dele);
i--;
}
if(list3.getSelectionBackground()==Color.cyan)
{
int dele=list3.getSelectedIndex();
m1.remove(dele);m2.remove(dele);
m3.remove(dele);m4.remove(dele);
i--;
}
if(list4.get
JonSco
- 粉丝: 91
- 资源: 1万+
最新资源
- 新能源汽车充电插口类型识别-YOLOV11标记,可识别Type1,ccs2的充电标准
- Git 2.47.1 arm64
- yolov5检测人脸和关键点,只依赖opencv库就可以运行,程序包含C++和Python两个版本的.zip
- yolov5目标检测模型的知识补充(基于响应的补充).zip
- 笔记自动驾驶预测与决策规划-Part8-数据驱动的规划方法
- 解决CAM350导入提示“找不到首标题%,载入停止”
- VPC网络管理-第21课 云平台网络基本概念-课件
- 新能源汽车充电插口类型识别-YOLOV8标记,可识别Type1,ccs2的充电标准
- 新能源汽车充电插口类型识别-YOLOV9标记,可识别Type1,ccs2的充电标准
- 新能源汽车充电插口类型识别-YOLOV7标记,可识别Type1,ccs2的充电标准
- YOLOv5(v6.0)的个人实现.zip
- yolov8 tensorrt 加速.zip
- 基于uni-app+uview-ui开发的校园云打印系统微信小程序项目源码+文档说明
- YOLOv8 使用 PyTorch、OpenCV 和 DeepSORT 进行对象跟踪和计数,部署在 Streamlit 上 .zip
- yolov8 瑞芯微 rknn 板端 C++ 配置 .zip
- yolov8n 部署版本,后期处理用python语言和C++语言形式进行改写,则移植不同的平台(onnx、tensorRT、RKNN、Horzion).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈