高手请进,菜鸟别进!来者有分,有关jTree的问题!在线等待!急!
来源: 互联网 发布时间:2017-03-24
本文导语: 先看看代码,: import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.borland.dx.sql.dataset.*; import com.borland.dbswing.*; import javax.swing.tree.*; /////////////////////////////////////////////////// public class main2Frame2 extends JFrame ...
先看看代码,:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.dx.sql.dataset.*;
import com.borland.dbswing.*;
import javax.swing.tree.*;
///////////////////////////////////////////////////
public class main2Frame2 extends JFrame implements ActionListener{
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JScrollPane jScrollPane1 = new JScrollPane();
private JTree jTree1;
private MenuBar menuBar1 = new MenuBar();
Menu menu1 = new Menu("文件");
MenuItem menuItem1 = new MenuItem("打开");
MenuItem menuItem2 = new MenuItem("保存");
MenuItem menuItem3 = new MenuItem("关闭");
Menu menu2 = new Menu("编辑");
MenuItem menuItem4 = new MenuItem("剪切");
MenuItem menuItem5 = new MenuItem("拷贝");
MenuItem menuItem6 = new MenuItem("粘贴");
Menu menu3 = new Menu("帮助");
MenuItem menuItem7 = new MenuItem("帮助");
MenuItem menuItem8= new MenuItem("关于我们");
private QueryDataSet queryDataSet1 = new QueryDataSet();
private TableScrollPane tableScrollPane1 = new TableScrollPane();
private JdbTable jdbTable1 = new JdbTable();
private Database database1 = new Database();
///////////////////////////////////////////////////////Construct the frame
public main2Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//////////////////////////////////////////////////////////Component initialization
private void jbInit() throws Exception {
this.setMenuBar(menuBar1);
menuItem1.setShortcut(new MenuShortcut(79));
menuItem2.setShortcut(new MenuShortcut(83));
menuItem3.setShortcut(new MenuShortcut(65, true));
menuItem5.setShortcut(new MenuShortcut(67));
menuItem6.setShortcut(new MenuShortcut(86));
menuItem7.setShortcut(new MenuShortcut(90, true));
menuItem8.setShortcut(new MenuShortcut(84, true));
menuItem4.setShortcut(new MenuShortcut(88));
menuBar1.add(menu1);
menuBar1.add(menu2);
menuBar1.add(menu3);
menu1.add(menuItem1);
menu1.add(menuItem2);
menu1.add(menuItem3);
menu2.add(menuItem4);
menu2.add(menuItem5);
menu2.add(menuItem6);
menu3.add(menuItem7);
menu3.add(menuItem8);
//////////////////////////////////////////////////
DefaultMutableTreeNode root = new DefaultMutableTreeNode("中小学学生查询系统");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("语文");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("数学");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("英语");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("信息");
DefaultMutableTreeNode node5 = new DefaultMutableTreeNode("其他");
///////////////////////////////////////////////////
DefaultTreeModel treeModel = new DefaultTreeModel(root);
treeModel.insertNodeInto(node1, root, root.getChildCount());
treeModel.insertNodeInto(node2, root, root.getChildCount());
treeModel.insertNodeInto(node3, root, root.getChildCount());
treeModel.insertNodeInto(node4, root, root.getChildCount());
treeModel.insertNodeInto(node5, root, root.getChildCount());
//////////////////////////////////////////////////////////////
jTree1 = new JTree(treeModel);
//////////////////////////////////////
//setIconImage(Toolkit.getDefaultToolkit().createImage(main2Frame2.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("中小学学生查询系统");
contentPane.add(jScrollPane1, BorderLayout.WEST);
contentPane.add(tableScrollPane1, BorderLayout.CENTER);
tableScrollPane1.getViewport().add(jdbTable1, null);
jScrollPane1.getViewport().add(jTree1, null);
///////////////////////////////////////
DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)jTree1.getCellRenderer();
cellRenderer.setLeafIcon(new ImageIcon("ip.gif"));
cellRenderer.setOpenIcon(new ImageIcon("text.gif"));
cellRenderer.setClosedIcon(new ImageIcon("ip.gif"));
///////////////////////////////////////
cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));
cellRenderer.setBackgroundNonSelectionColor(Color.white);
cellRenderer.setBackgroundSelectionColor(Color.yellow);
cellRenderer.setBorderSelectionColor(Color.red);
cellRenderer.setTextNonSelectionColor(Color.black);
cellRenderer.setTextSelectionColor(Color.blue);
///////////////////////////////////////
jTree1.setEditable(false);
database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master", "sa", "", false, "com.microsoft.jdbc.sqlserver.SQLServerDriver"));
database1.setDatabaseName("");
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "SELECT * FROM sysobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
/////////////////////////////////////////////
jTree1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
TreePath tp=jTree1.getPathForLocation(e.getX(),e.getY());
if(tp.toString().equals("语文")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(tp.toString().equals("数学")){//点击数学时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysaltfiles", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(tp.toString().equals("英语")){//点击英语时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscacheobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(tp.toString().equals("信息")){//点击信息时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscharsets", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
} }
});
/////////////////////////////////////////////////////////
////////// //Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.dx.sql.dataset.*;
import com.borland.dbswing.*;
import javax.swing.tree.*;
///////////////////////////////////////////////////
public class main2Frame2 extends JFrame implements ActionListener{
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JScrollPane jScrollPane1 = new JScrollPane();
private JTree jTree1;
private MenuBar menuBar1 = new MenuBar();
Menu menu1 = new Menu("文件");
MenuItem menuItem1 = new MenuItem("打开");
MenuItem menuItem2 = new MenuItem("保存");
MenuItem menuItem3 = new MenuItem("关闭");
Menu menu2 = new Menu("编辑");
MenuItem menuItem4 = new MenuItem("剪切");
MenuItem menuItem5 = new MenuItem("拷贝");
MenuItem menuItem6 = new MenuItem("粘贴");
Menu menu3 = new Menu("帮助");
MenuItem menuItem7 = new MenuItem("帮助");
MenuItem menuItem8= new MenuItem("关于我们");
private QueryDataSet queryDataSet1 = new QueryDataSet();
private TableScrollPane tableScrollPane1 = new TableScrollPane();
private JdbTable jdbTable1 = new JdbTable();
private Database database1 = new Database();
///////////////////////////////////////////////////////Construct the frame
public main2Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//////////////////////////////////////////////////////////Component initialization
private void jbInit() throws Exception {
this.setMenuBar(menuBar1);
menuItem1.setShortcut(new MenuShortcut(79));
menuItem2.setShortcut(new MenuShortcut(83));
menuItem3.setShortcut(new MenuShortcut(65, true));
menuItem5.setShortcut(new MenuShortcut(67));
menuItem6.setShortcut(new MenuShortcut(86));
menuItem7.setShortcut(new MenuShortcut(90, true));
menuItem8.setShortcut(new MenuShortcut(84, true));
menuItem4.setShortcut(new MenuShortcut(88));
menuBar1.add(menu1);
menuBar1.add(menu2);
menuBar1.add(menu3);
menu1.add(menuItem1);
menu1.add(menuItem2);
menu1.add(menuItem3);
menu2.add(menuItem4);
menu2.add(menuItem5);
menu2.add(menuItem6);
menu3.add(menuItem7);
menu3.add(menuItem8);
//////////////////////////////////////////////////
DefaultMutableTreeNode root = new DefaultMutableTreeNode("中小学学生查询系统");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("语文");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("数学");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("英语");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("信息");
DefaultMutableTreeNode node5 = new DefaultMutableTreeNode("其他");
///////////////////////////////////////////////////
DefaultTreeModel treeModel = new DefaultTreeModel(root);
treeModel.insertNodeInto(node1, root, root.getChildCount());
treeModel.insertNodeInto(node2, root, root.getChildCount());
treeModel.insertNodeInto(node3, root, root.getChildCount());
treeModel.insertNodeInto(node4, root, root.getChildCount());
treeModel.insertNodeInto(node5, root, root.getChildCount());
//////////////////////////////////////////////////////////////
jTree1 = new JTree(treeModel);
//////////////////////////////////////
//setIconImage(Toolkit.getDefaultToolkit().createImage(main2Frame2.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("中小学学生查询系统");
contentPane.add(jScrollPane1, BorderLayout.WEST);
contentPane.add(tableScrollPane1, BorderLayout.CENTER);
tableScrollPane1.getViewport().add(jdbTable1, null);
jScrollPane1.getViewport().add(jTree1, null);
///////////////////////////////////////
DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)jTree1.getCellRenderer();
cellRenderer.setLeafIcon(new ImageIcon("ip.gif"));
cellRenderer.setOpenIcon(new ImageIcon("text.gif"));
cellRenderer.setClosedIcon(new ImageIcon("ip.gif"));
///////////////////////////////////////
cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));
cellRenderer.setBackgroundNonSelectionColor(Color.white);
cellRenderer.setBackgroundSelectionColor(Color.yellow);
cellRenderer.setBorderSelectionColor(Color.red);
cellRenderer.setTextNonSelectionColor(Color.black);
cellRenderer.setTextSelectionColor(Color.blue);
///////////////////////////////////////
jTree1.setEditable(false);
database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master", "sa", "", false, "com.microsoft.jdbc.sqlserver.SQLServerDriver"));
database1.setDatabaseName("");
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "SELECT * FROM sysobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
/////////////////////////////////////////////
jTree1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
TreePath tp=jTree1.getPathForLocation(e.getX(),e.getY());
if(tp.toString().equals("语文")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(tp.toString().equals("数学")){//点击数学时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysaltfiles", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(tp.toString().equals("英语")){//点击英语时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscacheobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(tp.toString().equals("信息")){//点击信息时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscharsets", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
} }
});
/////////////////////////////////////////////////////////
////////// //Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
|
我回去看看,没用过JTree,还有就是最近一直加班,几乎没时间。^_^
不过,我会尽快回来给各答复的,能搞定或不能。 ^_^
不过,我会尽快回来给各答复的,能搞定或不能。 ^_^
|
修改如下:
jTree1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
TreePath tp=jTree1.getPathForLocation(e.getX(),e.getY());
String kk = tp.getLastPathComponent().toString();
if(kk.toString().equals("语文")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(kk.toString().equals("数学")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysaltfiles", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(kk.toString().equals("英语")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscacheobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(kk.toString().equals("信息")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscharsets", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
} }
});
jTree1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
TreePath tp=jTree1.getPathForLocation(e.getX(),e.getY());
String kk = tp.getLastPathComponent().toString();
if(kk.toString().equals("语文")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(kk.toString().equals("数学")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from sysaltfiles", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(kk.toString().equals("英语")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscacheobjects", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
}
if(kk.toString().equals("信息")){//点击语文时的响应
queryDataSet1.close();
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from syscharsets", null, true, Load.ALL));
jdbTable1.setDataSet(queryDataSet1);
} }
});