Java作業(yè):swing圖書管理系統(tǒng),JTree切換等的實現(xiàn),含免費自制透明背景的png圖片下載

手機上想要下載這些我用Ps軟件免費自制的透明背景png圖的話,只要長按圖片點擊專欄的圖片,然后點擊下載圖標就可以下載到你的手機上,以后會發(fā)很多免費自制素材到專欄的,求三連關(guān)注點贊?。∵@些png圖片在網(wǎng)站上都是要花錢買的!










package swingPackage;
public class DiaoYongFangFa {
public static void main(String[] args) {
/* swing類 */
new swingClass();
/* JTable界面 */
// new JTableJieMian();
}
}


package swingPackage;
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 javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class shijian implements ActionListener, FocusListener {
static swingClass d = null;
/* 下面的是構(gòu)造方法,無void */
public shijian(swingClass dl) {
d = dl;
}
@Override
/*
*
* actionperformed方法是ActionListener監(jiān)聽事件中的一個重寫方法,
*
* 如果要求按鈕執(zhí)行一些動作,可以在這個方法中添加、修改、刪除以及判斷。
*/
public void actionPerformed(ActionEvent arg0) {
/* 當按鈕中字符為"登錄"時,就執(zhí)行equals("登錄")中的內(nèi)容 */
if (arg0.getActionCommand().equals("已經(jīng)注冊過,可直接登錄")) {
/* trim()函數(shù)移除字符串兩側(cè)的空白字符或其他預(yù)定義字符。 */
String uname = swingClass.uname.getText().trim();
String pwd = swingClass.pwd.getText().trim();
if (uname.isEmpty() || pwd.isEmpty()) {
JOptionPane.showMessageDialog(null, "用戶名或密碼為空");
} else {
if (uname.equals(yonghu.uname) && pwd.equals(yonghu.pwd)) {
JOptionPane.showMessageDialog(null, "用戶名和密碼輸入正確,可以登錄");
new ChengGongDengLuHouDejiemian();
/* JTable界面 */
new JTableJieMian();
d.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用戶名或密碼輸入錯誤");
}
}
}
else if (arg0.getActionCommand().equals("沒注冊過,先注冊")) {
JOptionPane.showMessageDialog(null, "沒注冊過,先注冊");
} else {
int o = JOptionPane.showConfirmDialog(null, "是否確認關(guān)閉頁面");
if (o == 0) {
System.exit(0);
}
}
}
/*
*
* arg是java主函數(shù)的入口參數(shù),它是一個字符串數(shù)組變量 void focusGained(FocusEvent e):當獲得焦點時發(fā)生.
*
* void focusLost(FocusEvent e):當失去焦點時發(fā)生.
*/
@Override
public void focusGained(FocusEvent arg0) {
if (arg0.getSource() == d.uname) {
System.out.println("選中了用戶名框");
} else if (arg0.getSource() == d.pwd) {
System.out.println("選中了密碼框");
} else if (arg0.getSource() == d.pwd1) {
System.out.println("選中了確認密碼框");
}
}
@Override
public void focusLost(FocusEvent arg0) {
if (arg0.getSource() == d.uname) {
/* getSource獲得所選 */
String uname = d.uname.getText();
if (uname.isEmpty()) {
d.lb_uname_panduan.setText("請輸入用戶名");
d.lb_uname_panduan.setForeground(Color.red);
} else {
if (uname.length() > 12 || uname.length() == 0) {
d.lb_uname_panduan.setText("用戶名格式輸入錯誤");
d.lb_uname_panduan.setForeground(Color.red);
} else {
d.lb_uname_panduan.setText("用戶名輸入正確");
d.lb_uname_panduan.setForeground(Color.green);
}
}
} else if (arg0.getSource() == d.pwd) {
String pwd = d.pwd.getText().trim();
if (pwd.length() < 6 || pwd.length() == 0) {
d.lb_pwd_panduan.setText("密碼格式輸入錯誤,不可少于6位");
d.lb_pwd_panduan.setForeground(Color.red);
} else {
d.lb_pwd_panduan.setText("輸入正確");
d.lb_pwd_panduan.setForeground(Color.green);
}
} else if (arg0.getSource() == d.pwd1) {/*
* 做判斷兩次密碼是否一致的判斷
*
* 先獲取第一次輸入的密碼,再獲取第二次輸入的密碼,
* 然后比較一下兩次密碼是否一致不就完啦
*/
String pwd1 = d.pwd.getText().trim();// 獲取第一次的密碼
String pwd2 = d.pwd1.getText().trim();// 獲取第二次輸入的密碼.
if (pwd1.equals(pwd2)) {
d.lb_pwd_queren.setText("輸入正確");
d.lb_pwd_queren.setForeground(Color.GREEN);
} else {
d.lb_pwd_queren.setText("兩次密碼輸入不一致");
d.lb_pwd_queren.setForeground(Color.red);
}
}
}
}
public class swingClass extends JFrame {
static JCheckBox aihao1, aihao2, aihao3 = null;
static JComboBox com1 = null;
static JButton DengLuAnNu, QuXiaoAnNu, ZhuCeMiMaAnNu = null;
static JTextArea jt1 = null;
static JLabel lb_uname, lb_qrmima, lb_sex, lb_aihao, lb_address,
lb_jieshao = null;
static JLabel lb_uname_panduan, lb_pwd_panduan, lb_pwd,
lb_pwd_queren = null;
static JPasswordField pwd, pwd1 = null;
static JRadioButton sex_man, sex_woman = null;
static JTextField uname;
shijian sj = new shijian(this);
/* this就是指對象自己,表示自己,在那個對象的成員函數(shù)里出現(xiàn)就表示哪個對象 */
public swingClass() {
/* 總界面設(shè)置 */
this.setTitle("登錄界面");
this.setSize(700, 600);
this.setLocationRelativeTo(null);
this.setLayout(null);
/* 用戶名 */
lb_uname = new JLabel("用戶名");
this.add(lb_uname);
lb_uname.setBounds(100, 100, 40, 30);
/* setBounds(x,y,size_x,size_y) */
uname = new JTextField(20);
uname.setBounds(180, 100, 140, 30);
uname.addFocusListener(sj);
this.add(uname);
lb_uname_panduan = new JLabel("用戶名長度不能大于12位");
lb_uname_panduan.setForeground(Color.red);
lb_uname_panduan.setBounds(330, 100, 250, 30);
this.add(lb_uname_panduan);
/* 密 碼 */
lb_pwd = new JLabel("密 碼");
this.add(lb_pwd);
lb_pwd.setBounds(100, 140, 40, 30);
pwd = new JPasswordField();
pwd.setBounds(180, 140, 140, 30);
pwd.addFocusListener(sj);
this.add(pwd);
lb_pwd_panduan = new JLabel("請輸入密 碼,不可少與6位");
lb_pwd_panduan.setForeground(Color.red);
lb_pwd_panduan.setBounds(330, 140, 240, 30);
this.add(lb_pwd_panduan);
// lb_pwd_panduan = new JLabel("請確認密碼");
// lb_pwd_panduan.setForeground(Color.red);
// lb_pwd_panduan.setBounds(330, 140, 150, 30);
// this.add(lb_pwd_panduan);
/* 確認密碼 */
lb_qrmima = new JLabel("確認密碼");
lb_qrmima.setBounds(100, 180, 70, 30);
this.add(lb_qrmima);
pwd1 = new JPasswordField(20);
pwd1.setBounds(180, 180, 140, 30);
// pwd1.addFocusListener(sj);
pwd1.addFocusListener(new shijian(this));
this.add(pwd1);
lb_pwd_queren = new JLabel("請輸入和上一 次一致的密碼");
lb_pwd_queren.setForeground(Color.red);
lb_pwd_queren.setBounds(330, 180, 180, 30);
this.add(lb_pwd_queren);
lb_pwd_queren.addFocusListener(sj);
lb_sex = new JLabel("性別");
lb_sex.setBounds(100, 210, 70, 30);
this.add(lb_sex);
sex_woman = new JRadioButton("女");
sex_man = new JRadioButton("男", true);
sex_man.setBounds(180, 210, 70, 30);
sex_woman.setBounds(260, 210, 70, 30);
this.add(sex_man);
this.add(sex_woman);
ButtonGroup bg1 = new ButtonGroup();
bg1.add(sex_man);
bg1.add(sex_woman);
lb_aihao = new JLabel("愛好");
lb_aihao.setBounds(100, 250, 70, 30);
this.add(lb_aihao);
aihao1 = new JCheckBox("唱");
aihao2 = new JCheckBox("跳");
aihao3 = new JCheckBox("rap");
aihao1.setBounds(180, 250, 70, 30);
aihao2.setBounds(250, 250, 70, 30);
aihao3.setBounds(320, 250, 70, 30);
this.add(aihao1);
this.add(aihao2);
this.add(aihao3);
lb_address = new JLabel("家庭地址");
lb_address.setBounds(100, 290, 70, 30);
this.add(lb_address);
com1 = new JComboBox();
com1.addItem("請選擇家庭地址");
com1.addItem("北京");
com1.addItem("上海");
com1.addItem("廣州");
com1.setBounds(180, 290, 140, 30);
this.add(com1);
lb_jieshao = new JLabel("個人介紹");
lb_jieshao.setBounds(100, 330, 70, 30);
this.add(lb_jieshao);
jt1 = new JTextArea();
jt1.setColumns(8);
jt1.setRows(4);
jt1.setBounds(180, 330, 140, 40);
jt1.setLineWrap(true);
this.add(jt1);
JButton btn_dl = new JButton("已經(jīng)注冊過,可直接登錄");
btn_dl.setBounds(20, 400, 180, 35);
QuXiaoAnNu = new JButton("取消");
ZhuCeMiMaAnNu = new JButton("沒注冊過,先注冊");
ZhuCeMiMaAnNu.setBounds(368, 400, 180, 35);
QuXiaoAnNu.setBounds(240, 400, 80, 35);
btn_dl.addActionListener(sj);
ZhuCeMiMaAnNu.addActionListener(sj);
QuXiaoAnNu.addActionListener(sj);
this.add(btn_dl);
this.add(ZhuCeMiMaAnNu);
this.add(QuXiaoAnNu);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class yonghu {
static String pwd = "pwdpwd";
static String uname = "1";
}



package swingPackage;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class ChengGongDengLuHouDejiemian extends JFrame {
static JButton JButton1, JButton2, JButton3, JButton4 = null;
static JLabel JLabel1, JLabel2, JLabel3, JLabel4 = null;
static JPanel JPanel1, JPanel2, JPanel3, JPanel4, JPanel5, JPanel6 = null;
JTree JTree1 = null;// 聲明一個樹控件
public ChengGongDengLuHouDejiemian() {
this.setTitle("登錄后的主界面");
this.setLayout(null);
this.setSize(700, 700);
this.setLocationRelativeTo(null);
JPanel1 = new JPanel(null);
JButton1 = new JButton("切換用戶", new ImageIcon("img//1.png"));
JButton1.setVerticalTextPosition(JButton.TOP);
JButton1.setHorizontalTextPosition(JButton.CENTER);
JButton1.setBounds(100, 20, 100, 60);
JButton2 = new JButton("借閱排行榜", new ImageIcon("img//2.png"));
JButton2.setVerticalTextPosition(JButton.TOP);
JButton2.setHorizontalTextPosition(JButton.CENTER);
JButton2.setBounds(250, 20, 100, 60);
JButton3 = new JButton("系統(tǒng)管理", new ImageIcon("img//3.png"));
JButton3.setVerticalTextPosition(JButton.TOP);
JButton3.setHorizontalTextPosition(JButton.CENTER);
JButton3.setBounds(400, 20, 100, 60);
JButton4 = new JButton("退出系統(tǒng)", new ImageIcon("img//4.png"));
JButton4.setVerticalTextPosition(JButton.TOP);
JButton4.setHorizontalTextPosition(JButton.CENTER);
JButton4.setBounds(550, 20, 100, 60);
JPanel1.add(JButton1);
JPanel1.add(JButton2);
JPanel1.add(JButton3);
JPanel1.add(JButton4);
JPanel2 = new JPanel(null);
JPanel3 = new JPanel(null);
JPanel4 = new JPanel(null);
JPanel5 = new JPanel(null);
JPanel6 = new JPanel(null);
/* 給所有的JTree控件設(shè)置統(tǒng)一的圖標, 所以一定要在實例化JTree之前使用,否則沒有效果 */
UIManager.put("Tree.collapsedIcon", new ImageIcon("img//1.png"));// 默認打開圖標
UIManager.put("Tree.expandedIcon", new ImageIcon("img//2.png"));// 打開后圖標
UIManager.put("Tree.openIcon", new ImageIcon("img//3.png"));// 打開
UIManager.put("Tree.closedIcon", new ImageIcon("img//4.png"));// 關(guān)閉
UIManager.put("Tree.leafIcon", new ImageIcon("img//5.png"));// 打開后小圖標
DefaultMutableTreeNode dmtn1 = new DefaultMutableTreeNode("圖書管理");
DefaultMutableTreeNode dmtn_yonghu = new DefaultMutableTreeNode("用戶管理");
DefaultMutableTreeNode dmtn_yonghu_insert = new DefaultMutableTreeNode(
"增加用戶");
DefaultMutableTreeNode dmtn_yonghu_update = new DefaultMutableTreeNode(
"修改用戶");
DefaultMutableTreeNode dmtn_yonghu_delete = new DefaultMutableTreeNode(
"刪除用戶");
DefaultMutableTreeNode dmtn_yonghu_select = new DefaultMutableTreeNode(
"查詢用戶");
DefaultMutableTreeNode dmtn_jieyue = new DefaultMutableTreeNode("借閱管理");
DefaultMutableTreeNode dmtn_jieyue_insert = new DefaultMutableTreeNode(
"增加借閱信息");
DefaultMutableTreeNode dmtn_jieyue_update = new DefaultMutableTreeNode(
"修改借閱信息");
DefaultMutableTreeNode dmtn_jieyue_delete = new DefaultMutableTreeNode(
"刪除借閱信息");
DefaultMutableTreeNode dmtn_jieyue_select = new DefaultMutableTreeNode(
"查詢借閱信息");
// 從小到大的方向進行添加呢內(nèi)容
dmtn_yonghu.add(dmtn_yonghu_insert);
dmtn_yonghu.add(dmtn_yonghu_update);
dmtn_yonghu.add(dmtn_yonghu_delete);
dmtn_yonghu.add(dmtn_yonghu_select);
dmtn_jieyue.add(dmtn_jieyue_insert);
dmtn_jieyue.add(dmtn_jieyue_update);
dmtn_jieyue.add(dmtn_jieyue_delete);
dmtn_jieyue.add(dmtn_jieyue_select);
dmtn1.add(dmtn_yonghu);
dmtn1.add(dmtn_jieyue);
JTree1 = new JTree(dmtn1);
JTree1.addTreeSelectionListener(new shijian_zc(this));
JTree1.setBounds(10, 10, 150, 300);
JPanel2.add(JTree1);
JLabel1 = new JLabel();
JLabel2 = new JLabel();
JLabel3 = new JLabel();
JLabel4 = new JLabel();
JLabel1.setBounds(0, 0, 534, 564);
/* setBounds(x,y,size_x,size_y) */
JLabel2.setBounds(0, 0, 534, 564);
JLabel3.setBounds(0, 0, 534, 564);
JLabel4.setBounds(0, 0, 534, 564);
JLabel1.setIcon(new ImageIcon("img//11.png"));
JLabel2.setIcon(new ImageIcon("img//22.png"));
JLabel3.setIcon(new ImageIcon("img//33.png"));
JLabel4.setIcon(new ImageIcon("img//44.png"));
JPanel3.add(JLabel1);
JPanel4.add(JLabel2);
JPanel5.add(JLabel3);
JPanel6.add(JLabel4);
JButton1.addActionListener(new shijian_zc(this));
JButton2.addActionListener(new shijian_zc(this));
JButton3.addActionListener(new shijian_zc(this));
JButton4.addActionListener(new shijian_zc(this));
JPanel1.setBounds(0, 0, 700, 100);
JPanel2.setBounds(0, 100, 150, 600);
JPanel3.setBounds(150, 100, 550, 600);
JPanel4.setBounds(150, 100, 550, 600);
JPanel5.setBounds(150, 100, 550, 600);
JPanel6.setBounds(150, 100, 550, 600);
JPanel1.setBorder(BorderFactory.createLineBorder(Color.red));
JPanel2.setBorder(BorderFactory.createLineBorder(Color.green));
JPanel3.setBorder(BorderFactory.createLineBorder(Color.blue));
JPanel4.setBorder(BorderFactory.createLineBorder(Color.red));
JPanel5.setBorder(BorderFactory.createLineBorder(Color.green));
JPanel6.setBorder(BorderFactory.createLineBorder(Color.blue));
this.add(JPanel1);
this.add(JPanel2);
this.add(JPanel3);
this.add(JPanel4);
this.add(JPanel5);
this.add(JPanel6);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// this.setTitle("登錄后的界面");
// this.setLayout(null);
// this.setSize(800, 800);
// this.setLocationRelativeTo(null);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setVisible(true);
}
}
class shijian_zc implements ActionListener, TreeSelectionListener {
public ChengGongDengLuHouDejiemian dd = null;
public shijian_zc(ChengGongDengLuHouDejiemian d) {
dd = d;
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == dd.JButton1) {
dd.JPanel3.setVisible(true);// 讓jp3的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel4.setVisible(false);
dd.JPanel5.setVisible(false);
dd.JPanel6.setVisible(false);
} else if (arg0.getSource() == dd.JButton2) {
dd.JPanel4.setVisible(true);// 讓jp4的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel3.setVisible(false);
dd.JPanel5.setVisible(false);
dd.JPanel6.setVisible(false);
} else if (arg0.getSource() == dd.JButton3) {
dd.JPanel5.setVisible(true);// 讓jp5的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel4.setVisible(false);
dd.JPanel3.setVisible(false);
dd.JPanel6.setVisible(false);
} else if (arg0.getSource() == dd.JButton4) {
dd.JPanel6.setVisible(true);// 讓jp6的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel4.setVisible(false);
dd.JPanel5.setVisible(false);
dd.JPanel3.setVisible(false);
}
}
// 點擊Tree中的子節(jié)點會執(zhí)行的方法
@Override
public void valueChanged(TreeSelectionEvent arg0) {
// TODO Auto-generated method stub
// 獲取最后一次點擊的那個節(jié)點
DefaultMutableTreeNode str = (DefaultMutableTreeNode) dd.JTree1
.getLastSelectedPathComponent();
// JOptionPane.showMessageDialog(null, str.toString());
if (str.toString().equals("增加用戶")) {
dd.JPanel3.setVisible(true);// 讓jp3的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel4.setVisible(false);
dd.JPanel5.setVisible(false);
dd.JPanel6.setVisible(false);
} else if (str.toString().equals("修改用戶")) {
dd.JPanel4.setVisible(true);// 讓jp4的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel3.setVisible(false);
dd.JPanel5.setVisible(false);
dd.JPanel6.setVisible(false);
} else if (str.toString().equals("刪除用戶")) {
dd.JPanel5.setVisible(true);// 讓jp5的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel4.setVisible(false);
dd.JPanel3.setVisible(false);
dd.JPanel6.setVisible(false);
} else if (str.toString().equals("查詢用戶")) {
dd.JPanel6.setVisible(true);// 讓jp6的區(qū)域顯示,其他的區(qū)域隱藏
dd.JPanel4.setVisible(false);
dd.JPanel5.setVisible(false);
dd.JPanel3.setVisible(false);
}
}
}



package swingPackage;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableJieMian extends JFrame {
static DefaultTableModel dtm = null;// 儲存內(nèi)容的容器
/*
* 1.聲明表格 2.聲明數(shù)據(jù)源 (沒有數(shù)據(jù)庫,所以是模擬數(shù)據(jù)源) 3. 將每個集合的內(nèi)容添加到大集合(保存數(shù)據(jù)的集合)中 4.聲明一個儲存內(nèi)容的容器
* (defaultTableModel) 5.將內(nèi)容和標題添加到儲存內(nèi)容的容器里 6.將儲存內(nèi)容的容器添加到JTable中
* 7.標題不見了,內(nèi)容假如比較多,加個滾動條
*/
static JTable jt1 = null;// 聲明一個表格
// 這個集合用來存放內(nèi)容的標題
Vector<Object> v = new Vector<Object>();
// 存放具體的內(nèi)容的時候,因為內(nèi)容是多行多列,所以我們使用一個集合的集合存儲
Vector<Vector<Object>> v_neirong = new Vector<Vector<Object>>();
// 現(xiàn)在沒有數(shù)據(jù)庫,所以數(shù)據(jù)我們使用模擬數(shù)據(jù)庫
// List ArrayList Vector(和List一樣。對于JTable的操作的時候只能
// 使用這個集合)
// set HashSet
// Map HashMap
public JTableJieMian() {
// 布局方式、大小、居中、釋放資源、是否顯示
this.setTitle("豆瓣TOP評分9分以上書單");
v.add("編號");
v.add("圖書名稱");
v.add("價格");
v.add("出版社");
Vector<Object> v1 = new Vector<Object>();
v1.add(1);
v1.add("小王子");
v1.add("11.88元");
v1.add("人民文學出版社");
Vector<Object> v2 = new Vector<Object>();
v2.add(2);
v2.add("霍亂時期的愛情");
v2.add("20.0元");
v2.add("南海出版公司");
Vector<Object> v3 = new Vector<Object>();
v3.add(3);
v3.add("撒哈拉的故事");
v3.add("22.00元");
v3.add("皇冠出版社");
Vector<Object> v4 = new Vector<Object>();
v4.add(4);
v4.add("月亮和六便士");
v4.add("17.00元");
v4.add("上海譯文出版社");
Vector<Object> v5 = new Vector<Object>();
v5.add(5);
v5.add("活著");
v5.add("18.00元");
v5.add("南海出版公司");
Vector<Object> v6 = new Vector<Object>();
v6.add(6);
v6.add("百年孤獨");
v6.add("19.00元");
v6.add("南海出版公司");
Vector<Object> v7 = new Vector<Object>();
v7.add(7);
v7.add("平凡的世界");
v7.add("20.00元");
v7.add("人民文學出版社");
v_neirong.add(v1);
v_neirong.add(v2);
v_neirong.add(v3);
v_neirong.add(v4);
v_neirong.add(v5);
v_neirong.add(v6);
v_neirong.add(v7);
// dtm = new DefaultTableModel(v_neirong, v);// 將內(nèi)容儲存到容器中
dtm = new DefaultTableModel(v_neirong, v) {
@Override
public boolean isCellEditable(int row, int column) {
// 雙擊的時候就可以修改單個單元格,但是我們一-旦連上數(shù)據(jù)庫
// 你一修改內(nèi)容,數(shù)據(jù)庫沒有修改,那么數(shù)據(jù)就不真實
// 所以讓用戶禁止直接修改內(nèi)容
// 如何禁止,重寫其isCellEditable():是否允許用戶修改false
return false;
}
};
jt1 = new JTable(dtm);// 將儲存內(nèi)容的容器添加到表格中
jt1.setBounds(10, 10, 400, 270);
/* jt1. addMouseListener(new shijian_table(this));這種格式的必須寫不然會沒效果 */
jt1.addMouseListener(new shijian_table(this));
JScrollPane jsp = new JScrollPane(jt1);
jsp.setBounds(10, 10, 400, 100);
this.add(jsp);
this.setLayout(null);
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class shijian_table implements MouseListener {
public static JTableJieMian dt = null;
// 將窗體傳入過來:
public shijian_table(JTableJieMian dt) {
this.dt = dt;
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
int row = dt.jt1.getSelectedRow();// 獲取點擊的哪一行
String str = dt.jt1.getValueAt(row, 1).toString();
/* 1為下標,這里指的是或取第(1+1)行的文字 */
JOptionPane.showMessageDialog(null, str);
}
@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
}
}















