import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;public class MainFrame extends JFrame implements TreeSelectionListener, MouseListener {
private DefaultTreeModel model;
private JPopupMenu pop;
private JTree jtree;
JPopupMenu popup = new JPopupMenu();
JMenuItem item1 = new JMenuItem("修 改");
JMenuItem item2 = new JMenuItem("刷 新");
JMenuItem item3 = new JMenuItem("删 除"); private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = ""; // 数据库用户密码
private Panel borderpanel = new Panel();
private Panel flowpanel;
private JTextArea jta1 = new JTextArea("\n"+"序号:" +""+ "\t书名:" + "" + "\t副标题:" + ""
+ "\t作者:" + "" + "\t国籍:" + "" + "\t出版社:" + "" + "\t价格:" + ""+ "\tISBN:" + ""
+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"); public MainFrame(String title) throws SQLException, ClassNotFoundException {
super(title);
MenuBar mb = new MenuBar();
this.setMenuBar(mb);
Menu m1 = new Menu("菜单");
Menu m2 = new Menu("编辑");
Menu m3 = new Menu("关于");
mb.add(m1);
mb.add(m2);
mb.add(m3);
MenuItem mi1 = new MenuItem("添加一本书");
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddBook aa=new AddBook();
aa.setVisible(true);
}
}); MenuItem mi2 = new MenuItem("退出");
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
m1.add(mi1);
m1.add(mi2);
//获取屏幕分辨率
setSize(1280, 800);// 窗口大小1280*800
setLocation(0,0);// 距离左上角图标距离0,0
setLayout(new BorderLayout());
setLocationRelativeTo(null); //界面居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
TreeNode root =setJTree();
model = new DefaultTreeModel(root);
jtree = new JTree(model);
jtree.setEditable(true);
jtree.addMouseListener(this);
JScrollPane scrollPane = new JScrollPane(jtree);
add(scrollPane, BorderLayout.WEST);
setFlowLayoutPanel();
add(flowpanel);
add(jta1,BorderLayout.SOUTH); }
private TreeNode setJTree() {
jtree=new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("书籍收藏");
DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("收藏夹");
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("阅读列表");
DefaultMutableTreeNode root3 = new DefaultMutableTreeNode("搜索结果");
DefaultMutableTreeNode root4 = new DefaultMutableTreeNode("所有书籍");
DefaultMutableTreeNode root5 = new DefaultMutableTreeNode("回收站");
//ye-爷;fu-父;zi-子
DefaultMutableTreeNode ye = new DefaultMutableTreeNode("计算机类");
root.add(ye);
DefaultMutableTreeNode fu = new DefaultMutableTreeNode("数据库");
ye.add(fu);
DefaultMutableTreeNode zi = new DefaultMutableTreeNode("SQL数据库");
fu.add(zi);
zi = new DefaultMutableTreeNode("程序语言");
fu.add(zi);
fu = new DefaultMutableTreeNode("软件");
ye.add(fu);
zi = new DefaultMutableTreeNode("C语言");
fu.add(zi);
ye = new DefaultMutableTreeNode("经济类");
root.add(ye);
fu = new DefaultMutableTreeNode("宏观经济");
ye.add(fu);
fu = new DefaultMutableTreeNode("微观经济");
ye.add(fu);
root.add(root1);
root.add(root2);
root.add(root3);
root.add(root4);
root.add(root5);
//树的数据模型
DefaultTreeModel model = new DefaultTreeModel(root);
//设置数据模型
jtree.setModel(model);
// 展开所有树
for (int i = 0; i < jtree.getRowCount(); i++)
jtree.expandRow(i);
//添加事件
jtree.addTreeSelectionListener(this);
return root;
} @Override
public void valueChanged(TreeSelectionEvent arg0) {
// TODO Auto-generated method stub
} public void setFlowLayoutPanel() throws SQLException,
ClassNotFoundException {
flowpanel = new Panel();
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);//设置图标靠左
flowpanel.setLayout(layout);
popup.add(item1);//右键菜单
popup.add(item2);
popup.add(item3);
borderpanel.add(popup);
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user"; // SQL查询语句
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询 item1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
AddBook aa=new AddBook();
aa.setTitle("修改书籍");
aa.setVisible(true);
}
});
while (rs.next()) { // 遍历ResultSet
String str=rs.getString("url");
final String strid=rs.getString("id"); // 获取数据
final String strtitle=rs.getString("title");
final String strsubhead=rs.getString("subhead");
final String strauthors=rs.getString("authors");
final String strnationality=rs.getString("nationality");
final String strpublisher=rs.getString("publisher");
final String strprice=rs.getString("price");
final String strisbn=rs.getString("isbn");
JButton btnSmall=new JButton("");
ImageIcon icon = new ImageIcon(str);
btnSmall.setIcon(scaleImage(icon));
btnSmall.setSize(100, 150);
btnSmall.setVisible(true);
btnSmall.invalidate(); flowpanel.add(btnSmall); btnSmall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jta1.setText("\n"+"序号:" + strid + "\t书名:" + strtitle + "\t副标题:"
+ strsubhead + "\t作者:" + strauthors + "\t国籍:"
+ strnationality + "\t出版社:" + strpublisher
+ "\t价格:" + strprice + "\tISBN:" + strisbn+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n");
}
}); btnSmall.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event){
jta1.setText("\n"+"序号:" + strid + "\t书名:" + strtitle + "\t副标题:"
+ strsubhead + "\t作者:" + strauthors + "\t国籍:"
+ strnationality + "\t出版社:" + strpublisher
+ "\t价格:" + strprice + "\tISBN:" + strisbn+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n");
if(event.isPopupTrigger()){
popup.show(event.getComponent(),event.getX(),event.getY());
}
}
});
}
} private ImageIcon scaleImage(ImageIcon icon) { int width = icon.getIconWidth();
int height = icon.getIconHeight();
Image image = icon.getImage();
image = image.getScaledInstance(100, 150, Image.SCALE_DEFAULT);
return new ImageIcon(image);
} public static void main(String[] args) throws SQLException,
ClassNotFoundException {
MainFrame yf = new MainFrame("我的书架");
yf.setVisible(true);
} @Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
//获取选择的节点
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jtree.getLastSelectedPathComponent();
if(node.getUserObject() == "书籍收藏")
{
//显示提示信息
JOptionPane.showMessageDialog(null,
node.getUserObject()+" "+node.getChildCount());
}
else if(node.getUserObject() == "计算机类")
{
//显示提示信息
JOptionPane.showMessageDialog(null,
node.getUserObject()+" "+node.getChildCount());
}
else if(node.getUserObject() == "微观经济")
{
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user where classified=\'weiji\'"; // SQL查询语句
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
flowpanel.add(btnSmall);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(node.getUserObject() == "宏观经济")
{
//显示提示信息
JOptionPane.showMessageDialog(null, node.getUserObject()+" "+node.getChildCount());
}
}
@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
}
}
如想,我想首先点击JTree上面的子键,如微观经济学,就能用String sqlStr = "select * from user where classified=\'weiji\'语句筛选出数据库中的文件,并且以图标形式显示出来,但是问题来了:
1、窗口重叠,不知道如何清除原来的图标文件
2、是否可以调用一个方法就能解决问题,让所有子键都能搜索各自的内容
3、界面调整,我想让界面分成两行两列,JTree一列,flowpanel和jta1在一列上分上下
坐等指教
推荐GUI最强大的布局管理器网格袋布局GridBagLayout
掌握这个什么界面都能做出来
但是public void mouseClicked里面加的
String sqlStr = "select * from user where classified=\'weiji\'";
setFlowLayoutPanel();却运行不了
改一下,把转义去掉,这里不需要转义符
/*
Navicat MySQL Data TransferSource Server : localhost_3306
Source Server Version : 50150
Source Host : localhost:3306
Source Database : bookTarget Server Type : MYSQL
Target Server Version : 50150
File Encoding : 65001Date: 2011-07-17 21:14:07
*/SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`subhead` varchar(255) NOT NULL DEFAULT '',
`authors` varchar(255) DEFAULT NULL,
`nationality` varchar(255) DEFAULT NULL,
`publisher` varchar(255) DEFAULT NULL,
`classified` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`isbn` varchar(13) CHARACTER SET latin1 DEFAULT NULL,
`url` varchar(400) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=42 DEFAULT CHARSET=gbk ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO user VALUES ('1', '众包', '杰夫·豪', '大众力量缘何推动商业未来', '美国', '中信出版社', null, '38.00', '9787508615271', 'image\\9787508615271.jpg');
INSERT INTO user VALUES ('2', '维基经济学', '唐·泰普斯科特', '大规模协作如何改变一切', '加拿大', '中国青年出版社', null, '43.80', '9787500676911', 'image\\9787500676911.jpg');
INSERT INTO user VALUES ('3', 'facebook效应', '大卫·柯克帕特里克', '看fackbook如何打造无与伦比的社交帝国', '美国', '华文出版社', null, '49.80', '9787507532807', 'image\\9787507532807.jpg');
INSERT INTO user VALUES ('4', '长尾理论2.0', '克里斯·安德森', '扩容升级版', '美国', '中信出版社', null, '42.00', '9787508614977', 'image\\9787508614977.jpg');
INSERT INTO user VALUES ('5', '蓝海战略', 'W.钱·金', '超越产业竞争开创全新市场', '', '商务印书馆', null, '48.00', '9787100048088', 'image\\9787100048088.jpg');
INSERT INTO user VALUES ('6', '免费', '克里斯·安德森', '商业的未来', '', '中信出版社', null, '39.00', '9787508616384', 'image\\9787508616384.jpg');
INSERT INTO user VALUES ('7', '免费文化', '劳伦斯·莱斯格', '创意产业的未来', '', '中信出版社', null, '35.00', '9787508615882', 'image\\9787508615882.jpg');
INSERT INTO user VALUES ('8', '世界是平的', '托马斯·L·弗里德曼', '21世纪简史(内容升级和扩充版)', '', '湖南科学技术出版社', null, '58.00', '9787535753663', 'image\\9787535753663.jpg');
INSERT INTO user VALUES ('9', '正在爆发的互联网革命', '西门柳上', '', '', '机械工业出版社', null, '33.00', '9787111282259', 'image\\9787111282259.jpg');
INSERT INTO user VALUES ('10', '决策思维', '王嘉陵', 'IBM管理顾问丛书', '', '东方出版社', null, '28.00', '9787506033688', 'image\\9787506033688.jpg');
INSERT INTO user VALUES ('11', '财富的是与非', '亨利·刘易斯', '', '', '东方出版社', null, '26.00', '9787506035149', 'image\\9787506035149.jpg');
INSERT INTO user VALUES ('12', '大衰退', '辜朝明', '如何在金融危机中幸存和发展', '', '东方出版社', null, '39.00', '9787506033299', 'image\\9787506033299.jpg');
INSERT INTO user VALUES ('13', '杰克·韦尔奇自传', '', '', '', '', null, '', '9787508601915', 'image\\9787508601915.jpg');
INSERT INTO user VALUES ('14', '赢', '', '', '', '', null, '', '9787508603995', 'image\\9787508603995.jpg');
INSERT INTO user VALUES ('15', '赢的答案', '', '', '', '', null, '', '9787508609102', 'image\\9787508609102.jpg');
INSERT INTO user VALUES ('16', '公司的概念', '', '', '', '', null, '', '9787111185208', 'image\\9787111185208.jpg');
INSERT INTO user VALUES ('17', '管理的实践', '', '', '', '', null, '', '9787111177678', 'image\\9787111177678.jpg');
INSERT INTO user VALUES ('18', '成果管理', '', '', '', '', null, '', '9787111194675', 'image\\9787111194675.jpg');
INSERT INTO user VALUES ('19', '卓有成效的管理者', '', '', '', '', null, '', '9787111165170', 'image\\9787111165170.jpg');
INSERT INTO user VALUES ('20', '创新与企业家精神', '', '', '', '', null, '', '9787111199359', 'image\\9787111199359.jpg');
INSERT INTO user VALUES ('21', '动荡时代的管理', '', '', '', '', null, '', '9787111184775', 'image\\9787111184775.jpg');
INSERT INTO user VALUES ('22', '管理使命责任实务(使命篇)', '', '', '', '', null, '', '9787111188469', 'image\\9787111188469.jpg');
INSERT INTO user VALUES ('23', '管理使命责任实务(责任篇)', '', '', '', '', null, '', '9787111190349', 'image\\9787111190349.jpg');
INSERT INTO user VALUES ('24', '管理使命责任实务(实务篇)', '', '', '', '', null, '', '9787111188445', 'image\\9787111188445.jpg');
INSERT INTO user VALUES ('25', '德鲁克管理思想精要', '', '', '', '', null, '', '9787111215295', 'image\\9787111215295.jpg');
INSERT INTO user VALUES ('26', '管理前沿', '', '', '', '', null, '', '9787111190790', 'image\\9787111190790.jpg');
INSERT INTO user VALUES ('27', '管理未来', '', '', '', '', null, '', '9787111192879', 'image\\9787111192879.jpg');
INSERT INTO user VALUES ('28', '旁观者', '', '', '', '', null, '', '9787111171829', 'image\\9787111171829.jpg');
INSERT INTO user VALUES ('29', '21世纪的管理挑战', '', '', '', '', null, '', '9787111176190', 'image\\9787111176190.jpg');
INSERT INTO user VALUES ('30', '下一个社会的管理', '', '', '', '', null, '', '9787111194682', 'image\\9787111194682.jpg');
INSERT INTO user VALUES ('31', '巨变时代的管理', '', '', '', '', null, '', '9787111187004', 'image\\9787111187004.jpg');
INSERT INTO user VALUES ('32', '新企业战略', '', '', '', '', null, '', '9787508605906', 'image\\9787508605906.jpg');
INSERT INTO user VALUES ('33', '竞争战略', '', '', '', '', null, '', '9787508035093', 'image\\9787508035093.jpg');
INSERT INTO user VALUES ('34', '小沃森自传', '', '', '', '', null, '', '9787508602691', 'image\\9787508602691.jpg');
INSERT INTO user VALUES ('35', '谁说大象不能跳舞', '', '', '', '', null, '', '9787508606699', 'image\\9787508606699.jpg');
INSERT INTO user VALUES ('36', '论领导力', '', '', '', '', null, '', '9787508609041', 'image\\9787508609041.jpg');
INSERT INTO user VALUES ('37', '基业长青', '', '111', '', '', null, '', '9787800734762', 'image\\9787800734762.jpg');
INSERT INTO user VALUES ('38', '从优秀到卓越', '', '', '', '', null, '', '9787800735547', 'image\\9787800735547.jpg');
INSERT INTO user VALUES ('39', '追求卓越', '', '', '', '', null, '', '9787508608044', 'image\\9787508608044.jpg');
INSERT INTO user VALUES ('40', '彼得·林奇的成功投资(修订版)', '', '', '', '', null, '', '9787111200505', 'image\\9787111200505.jpg');
INSERT INTO user VALUES ('41', '创业学(第6版)', '11', '1111', '', '', null, '', '9787115138767', 'image\\9787115138767.jpg');
都在上面了,求解
MouseListener {
private Connection con=null;
private Statement st=null; private DefaultTreeModel model;
private JPopupMenu pop;
private JTree jtree;
JPopupMenu popup = new JPopupMenu(); JMenuItem item1 = new JMenuItem("修 改");
JMenuItem item2 = new JMenuItem("刷 新");
JMenuItem item3 = new JMenuItem("删 除"); private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = "root"; // 数据库用户密码,需要改成自己MySQL对应root的密码
private Panel borderpanel = new Panel();
private Panel flowpanel;
private JTextArea jta1 = new JTextArea("\n" + "序号:" + "" + "\t书名:" + ""
+ "\t副标题:" + "" + "\t作者:" + "" + "\t国籍:" + "" + "\t出版社:" + ""
+ "\t价格:" + "" + "\tISBN:" + "" + "\n" + "\n" + "\n" + "\n" + "\n"
+ "\n" + "\n" + "\n" + "\n" + "\n" + "\n");
private void init(){
try{
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
con = DriverManager.getConnection(dbURL, user, password); // 获取连接
st = con.createStatement(); // 获取Statement对象
}catch(Exception e){
e.printStackTrace();
}
} public MainFrame(String title) throws SQLException, ClassNotFoundException {
super(title);
init();
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
JMenu m1 = new JMenu("菜单");
JMenu m2 = new JMenu("编辑");
JMenu m3 = new JMenu("关于");
mb.add(m1);
mb.add(m2);
mb.add(m3);
JMenuItem mi1 = new JMenuItem("添加一本书");
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
}); JMenuItem mi2 = new JMenuItem("退出");
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
m1.add(mi1);
m1.add(mi2);
// 获取屏幕分辨率
//这里还需要用awt获取分辨率,待实现
setSize(1280, 800);// 窗口大小1280*800
setLocation(0, 0);// 距离左上角图标距离0,0
setLayout(new BorderLayout()); setLocationRelativeTo(null); // 界面居中
this.addWindowListener(new WindowAdapter(){ @Override
public void windowClosing(WindowEvent arg0) {
try{
if(st!=null)
st.close();
if(con!=null)
con.close();
System.exit(0);
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
});
//树组件的绘制
TreeNode root = setJTree(); model = new DefaultTreeModel(root);
jtree = new JTree(model);
jtree.setEditable(true);
jtree.addMouseListener(this);
JScrollPane scrollPane = new JScrollPane(jtree);
add(scrollPane, BorderLayout.WEST); setFlowLayoutPanel();
add(flowpanel);
add(jta1, BorderLayout.SOUTH); } private TreeNode setJTree() {
jtree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("书籍收藏");
DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("收藏夹");
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("阅读列表");
DefaultMutableTreeNode root3 = new DefaultMutableTreeNode("搜索结果");
DefaultMutableTreeNode root4 = new DefaultMutableTreeNode("所有书籍");
DefaultMutableTreeNode root5 = new DefaultMutableTreeNode("回收站");
// ye-爷;fu-父;zi-子
DefaultMutableTreeNode ye = new DefaultMutableTreeNode("计算机类");
root.add(ye);
DefaultMutableTreeNode fu = new DefaultMutableTreeNode("数据库");
ye.add(fu);
DefaultMutableTreeNode zi = new DefaultMutableTreeNode("SQL数据库");
fu.add(zi);
zi = new DefaultMutableTreeNode("程序语言");
fu.add(zi);
fu = new DefaultMutableTreeNode("软件");
ye.add(fu);
zi = new DefaultMutableTreeNode("C语言");
fu.add(zi);
ye = new DefaultMutableTreeNode("经济类");
root.add(ye);
fu = new DefaultMutableTreeNode("宏观经济");
ye.add(fu); fu = new DefaultMutableTreeNode("微观经济");
ye.add(fu);
root.add(root1);
root.add(root2);
root.add(root3);
root.add(root4);
root.add(root5);
// 树的数据模型
DefaultTreeModel model = new DefaultTreeModel(root);
// 设置数据模型
jtree.setModel(model);
// 展开所有树
for (int i = 0; i < jtree.getRowCount(); i++)
jtree.expandRow(i);
// 添加事件
jtree.addTreeSelectionListener(this); return root; } @Override
public void valueChanged(TreeSelectionEvent arg0) { } public void setFlowLayoutPanel() throws SQLException,
ClassNotFoundException {
flowpanel = new Panel();
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);// 设置图标靠左
flowpanel.setLayout(layout); popup.add(item1);// 右键菜单
popup.add(item2);
popup.add(item3);
borderpanel.add(popup); String sqlStr = "select * from user"; // SQL查询语句 ResultSet rs = st.executeQuery(sqlStr); // 执行查询 item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { }
});
//显示每一本书
while (rs.next()) { // 遍历ResultSet
String str = rs.getString("url");
final String strid = rs.getString("id"); // 获取数据
final String strtitle = rs.getString("title");
final String strsubhead = rs.getString("subhead");
final String strauthors = rs.getString("authors");
final String strnationality = rs.getString("nationality");
final String strpublisher = rs.getString("publisher");
final String strprice = rs.getString("price");
final String strisbn = rs.getString("isbn");
JButton btnSmall = new JButton(strtitle);
btnSmall.setSize(400, 150);
btnSmall.setVisible(true);
btnSmall.invalidate(); flowpanel.add(btnSmall); btnSmall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jta1.setText("\n" + "序号:" + strid + "\t书名:" + strtitle
+ "\t副标题:" + strsubhead + "\t作者:" + strauthors
+ "\t国籍:" + strnationality + "\t出版社:"
+ strpublisher + "\t价格:" + strprice + "\tISBN:"
+ strisbn + "\n" + "\n" + "\n" + "\n" + "\n" + "\n"
+ "\n" + "\n" + "\n" + "\n" + "\n"); }
}); btnSmall.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
jta1.setText("\n" + "序号:" + strid + "\t书名:" + strtitle
+ "\t副标题:" + strsubhead + "\t作者:" + strauthors
+ "\t国籍:" + strnationality + "\t出版社:"
+ strpublisher + "\t价格:" + strprice + "\tISBN:"
+ strisbn + "\n" + "\n" + "\n" + "\n" + "\n" + "\n"
+ "\n" + "\n" + "\n" + "\n" + "\n");
if (event.isPopupTrigger()) {
popup.show(event.getComponent(), event.getX(),
event.getY());
}
}
}); }
} private ImageIcon scaleImage(ImageIcon icon) { int width = icon.getIconWidth();
int height = icon.getIconHeight();
Image image = icon.getImage();
image = image.getScaledInstance(100, 150, Image.SCALE_DEFAULT);
return new ImageIcon(image);
} public static void main(String[] args) throws SQLException,
ClassNotFoundException {
MainFrame yf = new MainFrame("我的书架");
yf.setVisible(true);
} @Override
public void mouseClicked(MouseEvent arg0) {
// 获取选择的节点
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jtree
.getLastSelectedPathComponent();
if(!node.isLeaf()){
return ;
}
flowpanel.removeAll();
System.out.println(node.getUserObject());
String sqlStr="select * from user where classified ='"+node.getUserObject().toString()+"'";
try{
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
while (rs.next()) { // 遍历ResultSet
String str = rs.getString("url");
final String strid = rs.getString("id"); // 获取数据
final String strtitle = rs.getString("title");
final String strsubhead = rs.getString("subhead");
final String strauthors = rs.getString("authors");
final String strnationality = rs.getString("nationality");
final String strpublisher = rs.getString("publisher");
final String strprice = rs.getString("price");
final String strisbn = rs.getString("isbn");
JButton btnSmall = new JButton(strtitle);
btnSmall.setSize(400, 150);
btnSmall.setVisible(true);
btnSmall.invalidate(); flowpanel.add(btnSmall); btnSmall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jta1.setText("\n" + "序号:" + strid + "\t书名:" + strtitle
+ "\t副标题:" + strsubhead + "\t作者:" + strauthors
+ "\t国籍:" + strnationality + "\t出版社:"
+ strpublisher + "\t价格:" + strprice + "\tISBN:"
+ strisbn + "\n" + "\n" + "\n" + "\n" + "\n" + "\n"
+ "\n" + "\n" + "\n" + "\n" + "\n"); }
}); btnSmall.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
jta1.setText("\n" + "序号:" + strid + "\t书名:" + strtitle
+ "\t副标题:" + strsubhead + "\t作者:" + strauthors
+ "\t国籍:" + strnationality + "\t出版社:"
+ strpublisher + "\t价格:" + strprice + "\tISBN:"
+ strisbn + "\n" + "\n" + "\n" + "\n" + "\n" + "\n"
+ "\n" + "\n" + "\n" + "\n" + "\n");
if (event.isPopupTrigger()) {
popup.show(event.getComponent(), event.getX(),
event.getY());
}
}
}); }
}catch(Exception e){
e.printStackTrace();
}
flowpanel.repaint();
flowpanel.validate();
} public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { }}
第一,运行这个工程需要数据表有数据,你原来表里面的classified这一个字段可以设置几个如“宏观经济”或“微观经济”,否则点击树状菜单都没有数据
第二,你代码里面的AddBook这个类相关我都注解掉,影响到了插入的逻辑(因为我没那个类也没改写),你自己加上吧。
第三,对于树状菜单的“我的收藏”等不适合classified字段的侦听没有实现,建议另起一个tree,不要跟书籍分类混杂一起,逻辑不好处理。你说的问题处理起来逻辑都在代码里面,大体过程就是移除右边面板的所有按钮removeAll(),然后利用数据库连接创建按钮,添加进入面板,然后让面板重新绘制 repaint() validate()。
至于说所有的节点统一的处理,就是获得tree的leaf节点,获得节点的名称,拼接SQL语句,查询,所以classfied设置要跟节点的名称一样代码有很多值得优化的,先给你这个版本吧。