java getselectedrow_Java swing(2)自己设计完善的独特功能图书管理系统,获取下拉框等的文字内容等...

这是一个使用Java Swing构建的图书管理系统,实现了获取下拉框和表格双击事件中文字内容的功能。用户可以添加图书信息,并通过双击表格中的某一行来编辑相关信息,包括书籍的编号、名称、价格、类型ID和介绍。同时,系统提供了删除功能。
摘要由CSDN通过智能技术生成

总框架:81ae5e33a2c3ce77da06bb35f5552e41.png

本篇所含框架:e47df630dfdffad5c78b389728a0924a.png

f3538f97cb4abb05e3ec1e16e15a232f.png

package denglu;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Vector;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

public class puTongYongHuJieMian extends JFrame {

class shijian_table implements MouseListener, ActionListener {

public puTongYongHuJieMian HanDengLuHouJieMianNeiRong = null;

public shijian_table(puTongYongHuJieMian dengLuHouJieMianNeiRong) {

this.HanDengLuHouJieMianNeiRong = dengLuHouJieMianNeiRong;

}

// HanDengLuHouJieMianNeiRong(含登录后界面内容)

// DengLuHouJieMianNeiRong(登录后界面内容)

// 把窗体等内容都赋值传给HanDengLuHouJieMianNeiRong是为了方便下面如

// HanDengLuHouJieMianNeiRong.txt_name.getText();的“点(.)式引用,由“.”

// 后面的精确后缀条件提示等引用精确内容”

@Override

public void actionPerformed(ActionEvent arg0) {

String name = HanDengLuHouJieMianNeiRong.wenBenKuang_name.getText();

String price = HanDengLuHouJieMianNeiRong.wenBenKuang_price

.getText();

String type = HanDengLuHouJieMianNeiRong.wenBenKuang_typeid

.getText();

String jieshao = HanDengLuHouJieMianNeiRong.wenBenKuang_spjieshao

.getText();

// ArrayList jiHe = new ArrayList();

// // jiHe集合

//

// if (HanDengLuHouJieMianNeiRong.address1.isSelected()) {

// jiHe.add(HanDengLuHouJieMianNeiRong.address1.getText());

// }

// if (HanDengLuHouJieMianNeiRong.address2.isSelected()) {

// jiHe.add(HanDengLuHouJieMianNeiRong.address2.getText());

// }

// if (HanDengLuHouJieMianNeiRong.address3.isSelected()) {

// jiHe.add(HanDengLuHouJieMianNeiRong.address3.getText());

// }

// String typeName = HanDengLuHouJieMianNeiRong.xiaLaKuang

// .getSelectedItem().toString();

String sql = "insert into tushu values('" + name + "'" + ","

+ price + "," + type + ",'" + jieshao + "')";

// String sql2 = "insert into ts_type(ts_typename) values('"

// + typeName + "')";

// if (DBUtils.ZSG(sql) && DBUtils.ZSG(sql2)) {

if (DBUtils.ZSG(sql)) {

JOptionPane.showMessageDialog(null, "增加成功");

HanDengLuHouJieMianNeiRong.chaxunchushihua();

} else {

JOptionPane.showMessageDialog(null, "出现了未知的错误,增加失败");

}

}

@Override

public void mouseClicked(MouseEvent arg0) {

if (arg0.getClickCount() == 2) {

int row = HanDengLuHouJieMianNeiRong.biaoGe1.getSelectedRow();

String leixing = HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 4).toString();

HanDengLuHouJieMianNeiRong.wenBenKuang_Bianhao

.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 0).toString());

HanDengLuHouJieMianNeiRong.wenBenKuang_name

.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 1).toString());

HanDengLuHouJieMianNeiRong.wenBenKuang_price

.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 2).toString());

HanDengLuHouJieMianNeiRong.wenBenKuang_typeid

.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 3).toString());

HanDengLuHouJieMianNeiRong.wenBenKuang_spjieshao

.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 5).toString());

HanDengLuHouJieMianNeiRong.wenBenKuang_typename

.setText(leixing);

// 给下拉框内容赋值:

for (int i = 0; i < HanDengLuHouJieMianNeiRong.xiaLaKuang

.getItemCount(); i++) {

if (HanDengLuHouJieMianNeiRong.xiaLaKuang.getItemAt(i)

.toString().equals(leixing)) {

HanDengLuHouJieMianNeiRong.xiaLaKuang

.setSelectedItem(leixing);

}

}

}

if (arg0.isMetaDown()) {

int num = JOptionPane.showConfirmDialog(null, "是否确认删除这条信息?");

if (num == 0) {

int row = HanDengLuHouJieMianNeiRong.biaoGe1

.getSelectedRow();

String sql = "delete tushu where ts_id="

+ HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(

row, 0) + "";

if (DBUtils.ZSG(sql)) {

JOptionPane.showMessageDialog(null, "册除成功");

HanDengLuHouJieMianNeiRong.chaxunchushihua();

// 删除成功再查询一遍表格就可以实现表格的刷新,但是我们的查询写在了构造方法里,内容很多,

// 这里想要使用又要在写一遍,很麻烦,那么现在我们写一个方法 将查询内容封装进去,再想查询的

// 时候只要调用查询的方法即可

} else {

JOptionPane.showMessageDialog(null, "出现了未知的错误,请重试");

}

}

}

}

@Override

public void mouseEntered(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent arg0) {

// TODO Auto-generated method stub

}

}

static JButton anNiu_zengJia = null;

// gunDongTiao(滚动条)

static JTable biaoGe1 = null;

// xiaLaKuang(下拉框)

static DefaultTableModel biaoGeMoXing1 = null;

// biaoGeMoXing1(表格模型1)

static JScrollPane gunDongTiao = null;

// 集合(jiHe),标题(biaoti)

static Vector> Vector_jiHe_biaoGeNeiRong = null;

// wenBenKuang(文本框)

static Vector Vector_jiHe_biaoti = null;

// 文字(wenZi)

static JTextField wenBenKuang_Bianhao, wenBenKuang_name, wenBenKuang_price,

wenBenKuang_typeid, wenBenKuang_spjieshao, wenBenKuang_typename;

// biaoGe1(表格1)

static JLabel wenZi_Bianhao, wenZi_name, wenZi_price, wenZi_typeid,

wenZi_tjtypeName_xiaLaKuang, wenZi_spjieshao, wenZi_tjtypeName;

// anNiu_zengJia按钮_增加

public static JComboBox xiaLaKuang = null;

// biaoGeNeiRong(表格内容)

JPanel mianBan1, mianBan2 = null;

// mianBan(面板)

public puTongYongHuJieMian() {

this.setTitle("普通用户登录后的界面");

this.setSize(800, 600);

this.setLayout(null);

this.setLocationRelativeTo(null);

wenZi_Bianhao = new JLabel("添加或查找编号");

wenZi_name = new JLabel("添加或查找名称");

wenZi_price = new JLabel("添加或查找价格");

wenZi_tjtypeName = new JLabel("添加或查找类型名称");

wenZi_tjtypeName_xiaLaKuang = new JLabel("查找类型名称");

wenZi_typeid = new JLabel("添加或查找类型ID");

wenZi_spjieshao = new JLabel("添加或查找介绍");

anNiu_zengJia = new JButton("添加数据");

anNiu_zengJia.setBounds(530, 390, 100, 30);

anNiu_zengJia.addActionListener(new shijian_table(this));

this.add(anNiu_zengJia);

wenZi_Bianhao.setBounds(510, 100, 100, 30);

wenZi_name.setBounds(510, 140, 100, 30);

wenZi_price.setBounds(510, 180, 100, 30);

wenZi_typeid.setBounds(510, 220, 130, 30);

wenZi_spjieshao.setBounds(510, 260, 100, 30);

wenZi_tjtypeName.setBounds(510, 300, 170, 30);

wenZi_tjtypeName_xiaLaKuang.setBounds(510, 340, 170, 30);

this.add(wenZi_Bianhao);

this.add(wenZi_name);

this.add(wenZi_price);

this.add(wenZi_typeid);

this.add(wenZi_spjieshao);

this.add(wenZi_tjtypeName_xiaLaKuang);

this.add(wenZi_tjtypeName);

wenBenKuang_Bianhao = new JTextField();// 画框框的界面

wenBenKuang_Bianhao.setEditable(false);

wenBenKuang_name = new JTextField();

wenBenKuang_price = new JTextField();

wenBenKuang_typeid = new JTextField();

wenBenKuang_spjieshao = new JTextField();

wenBenKuang_typename = new JTextField();

wenBenKuang_Bianhao.setBounds(640, 100, 130, 30);

wenBenKuang_name.setBounds(640, 140, 130, 30);

wenBenKuang_price.setBounds(640, 180, 130, 30);

wenBenKuang_typeid.setBounds(640, 220, 130, 30);

wenBenKuang_spjieshao.setBounds(640, 260, 130, 30);

wenBenKuang_typename.setBounds(640, 300, 130, 30);

this.add(wenBenKuang_Bianhao);

this.add(wenBenKuang_name);

this.add(wenBenKuang_price);

this.add(wenBenKuang_typeid);

this.add(wenBenKuang_spjieshao);

this.add(wenBenKuang_typename);

Vector_jiHe_biaoti = new Vector();

Vector_jiHe_biaoti.add("编号");

Vector_jiHe_biaoti.add("名称");

Vector_jiHe_biaoti.add("价格");

Vector_jiHe_biaoti.add("类型ID");

Vector_jiHe_biaoti.add("类型名称");

Vector_jiHe_biaoti.add("介绍");

String sql = "select * from tushu as a inner join ts_Type as"

+ " b on(a.ts_TypeID=b.ts_TypeID)";

ResultSet res = DBUtils.Select(sql);

try {

Vector_jiHe_biaoGeNeiRong = new Vector>();

while (res.next()) {

Vector v = new Vector();

v.add(res.getInt("ts_ID"));

v.add(res.getString("ts_Name"));

v.add(res.getDouble("ts_price"));

v.add(res.getInt("ts_TypeID"));

v.add(res.getString("ts_TypeName"));

v.add(res.getString("ts_Jieshao"));

Vector_jiHe_biaoGeNeiRong.add(v);

biaoGeMoXing1 = new DefaultTableModel(

Vector_jiHe_biaoGeNeiRong, Vector_jiHe_biaoti);

}

biaoGeMoXing1 = new DefaultTableModel(Vector_jiHe_biaoGeNeiRong,

Vector_jiHe_biaoti) {

@Override

public boolean isCellEditable(int a, int b) {

return false;

}

};

biaoGe1 = new JTable(biaoGeMoXing1);

biaoGe1.addMouseListener(new shijian_table(this));

biaoGe1.setBounds(0, 0, 400, 500);

gunDongTiao = new JScrollPane(biaoGe1);

gunDongTiao.setBounds(0, 0, 450, 150);

mianBan1 = new JPanel();/*

* JPanel1 = new JPanel(); 不可在gundong = new

* JScrollPane(JTable1);上面

*/

mianBan1.add(gunDongTiao);

// 加this.add(gundong);删mianBan1.add(gunDongTiao );会有直接的滚动条,但变只可删一次

mianBan1.setBounds(0, 0, 450, 250);

this.add(mianBan1);

} catch (SQLException e) {

e.printStackTrace();

}

shangPinLeiXingXiaLaKuang();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

// 这里引用chaxunchushihua();没作用

}

// public void XXX() 格式就是封装查询的方法,可调用,无加静态属性的单词

public void chaxunchushihua() {

// 判断JPanel是不是为空

if (this.mianBan1 != null) {

// 面板1不为空说明面板1中有内容,就把面板1内容用从this中用remove移除,下面再加新内容

this.remove(mianBan1);

}

Vector_jiHe_biaoti = new Vector();

Vector_jiHe_biaoti.add("编号");

Vector_jiHe_biaoti.add("名称");

Vector_jiHe_biaoti.add("价格");

Vector_jiHe_biaoti.add("类型ID");

Vector_jiHe_biaoti.add("类型名称");

Vector_jiHe_biaoti.add("介绍");

String sql = "select * from tushu as a inner join ts_Type as"

+ " b on(a.ts_TypeID=b.ts_TypeID)";

ResultSet res = DBUtils.Select(sql);

try {

Vector_jiHe_biaoGeNeiRong = new Vector>();

while (res.next()) {

Vector v = new Vector();

v.add(res.getInt("ts_ID"));

v.add(res.getString("ts_Name"));

v.add(res.getDouble("ts_price"));

v.add(res.getInt("ts_TypeID"));

v.add(res.getString("ts_TypeName"));

v.add(res.getString("ts_Jieshao"));

Vector_jiHe_biaoGeNeiRong.add(v);

biaoGeMoXing1 = new DefaultTableModel(

Vector_jiHe_biaoGeNeiRong, Vector_jiHe_biaoti);

}

biaoGeMoXing1 = new DefaultTableModel(Vector_jiHe_biaoGeNeiRong,

Vector_jiHe_biaoti) {

@Override

public boolean isCellEditable(int a, int b) {

return false;

}

};

biaoGe1 = new JTable(biaoGeMoXing1);

// XXX.addMouseListener(new XXX(this));为给表格添加鼠标点击事件的格式,多总结和记录格式等法

biaoGe1.addMouseListener(new shijian_table(this));

gunDongTiao = new JScrollPane(biaoGe1);

gunDongTiao.setBounds(0, 0, 450, 150);

mianBan1 = new JPanel();

mianBan1.add(gunDongTiao);

mianBan1.setBounds(0, 0, 450, 250);

this.add(mianBan1);

} catch (SQLException e) {

e.printStackTrace();

}

// 释放资源:this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

// shangPinLeiXingXiaLaKuang(商品类型下拉框)

public void shangPinLeiXingXiaLaKuang() {

String sql = "select * from ts_type";

ResultSet res = DBUtils.Select(sql);

xiaLaKuang = new JComboBox();

try {

while (res.next()) {

// com1. setSelectedIndex(Integer.parseInt(res. getObject(1).

// toString()));

xiaLaKuang.addItem(res.getObject(2).toString());

// xiaLaKuang.addItem(res.getObject(1).toString());

}

} catch (SQLException e) {

e.printStackTrace();

}

xiaLaKuang.setBounds(640, 340, 130, 30);

this.add(xiaLaKuang);

}

}

23ed15fa8ef33e9ac17102c68c15e6ce.png

60cb9f4d97388275fb34e64e946de8fe.png

package denglu;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

class shijian_zhmm implements FocusListener, ActionListener {

public zhaoHuiMiMa shijian_zhaoHuiMiMa1 = null;

public shijian_zhmm(zhaoHuiMiMa shijian_zhaoHuiMiMa2) {

this.shijian_zhaoHuiMiMa1 = shijian_zhaoHuiMiMa2;

}

@Override

public void actionPerformed(ActionEvent arg0) {

String String_uname = shijian_zhaoHuiMiMa1.txt_uname.getText().trim();

String String_wenti = shijian_zhaoHuiMiMa1.txt_wenti.getText().trim();

String String_daan = shijian_zhaoHuiMiMa1.txt_daan.getText().trim();

String sql = "select * from yonghu where yh_Uname='" + String_uname

+ "'and yh_Q='" + String_wenti + "'and yh_A='" + String_daan

+ "'";

ResultSet res = DBUtils.Select(sql);

try {

if (res.next()) {

new chongzhimimajiemian();

gongjvClass.uname = String_uname;

} else {

// JOptionPane.showMessageDialog(null,"XXX")是用来提示用户答案输入错误的格式

JOptionPane.showMessageDialog(null, "答案输入错误");

shijian_zhaoHuiMiMa1.lb_daan1.setText("答案输入错误");

shijian_zhaoHuiMiMa1.lb_daan1.setForeground(Color.red);

}

} catch (SQLException e) {

e.printStackTrace();

}

}

@Override

public void focusGained(FocusEvent arg0) {

// focusGained(FocusEvent arg0)是 失去焦点事件的格式

}

@Override

public void focusLost(FocusEvent arg0) {

String uname = shijian_zhaoHuiMiMa1.txt_uname.getText().trim();

String sql = "select * from yonghu where yh_uname='" + uname + "'";

ResultSet res = DBUtils.Select(sql);

try {

if (res.next()) {

String shiJian_wenti = res.getString("yh_Q");

shijian_zhaoHuiMiMa1.txt_wenti.setText(shiJian_wenti);

shijian_zhaoHuiMiMa1.lb_uname1.setText("");

} else {

shijian_zhaoHuiMiMa1.lb_uname1.setText("用户名错误");

shijian_zhaoHuiMiMa1.lb_uname1.setForeground(Color.red);

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public class zhaoHuiMiMa extends JFrame {

static JButton btn_queren, btn_quxiao = null;

static JLabel lb_uname, lb_wenti, lb_daan = null;

static JLabel lb_uname1, lb_wenti1, lb_daan1 = null;

/*

* 找回密码思路:1.写一个文本框用来填用户名,用户名需要添加焦点获取事件,如果失去焦点后去数据库查询用户输入

* 的用户名是否存在,如果存在就将其问题查询出来,添加到第二 个文本框上(第二个文本框是不能手动输

* 入的),让用户输入答案,输入完毕后点击确定按钮时判断用户名,问题和答案是否一致, 如果一致说明 用户输入的答案是正确的, 之后跳到重置密码界面

*/

static JTextField txt_uname, txt_wenti, txt_daan = null;

public zhaoHuiMiMa() {

// 基本必须的内容:布局方式、大小居中、释放资源(找回密码页面不需要释放资源)、显示

// 布局方式

this.setTitle("找回密码页面");

this.setLayout(null);

// 大小居中

this.setSize(500, 500);

this.setLocationRelativeTo(null);

lb_uname = new JLabel("用户名");

lb_wenti = new JLabel("问题");

lb_daan = new JLabel("答案");

lb_uname1 = new JLabel("请输入用户名");

lb_daan1 = new JLabel("请输入答案");

txt_uname = new JTextField();

txt_wenti = new JTextField();

txt_daan = new JTextField();

txt_uname.addFocusListener(new shijian_zhmm(this));

lb_uname1.setForeground(Color.blue);

lb_daan1.setForeground(Color.blue);

lb_uname.setBounds(100, 100, 100, 30);

lb_wenti.setBounds(100, 140, 100, 30);

lb_daan.setBounds(100, 180, 100, 30);

lb_uname1.setBounds(310, 105, 100, 30);

lb_daan1.setBounds(310, 185, 100, 30);

this.add(lb_uname);

this.add(lb_wenti);

this.add(lb_daan);

this.add(lb_uname1);

this.add(lb_daan1);

txt_uname.setBounds(210, 100, 100, 30);

txt_wenti.setBounds(210, 140, 100, 30);

txt_wenti.setEditable(false);

txt_daan.setBounds(210, 180, 100, 30);

this.add(txt_uname);

this.add(txt_wenti);

this.add(txt_daan);

btn_queren = new JButton("确认且回答正确后跳转重置密码页面");

btn_queren.addActionListener(new shijian_zhmm(this));

btn_quxiao = new JButton("取消");

btn_queren.setBounds(10, 230, 290, 30);

btn_quxiao.setBounds(340, 230, 70, 30);

this.add(btn_queren);

this.add(btn_quxiao);

// 显示

this.setVisible(true);

}

}

8c4982b05d55edd19e9bf1d92b1a580e.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值