📄 multipledataclassframe2.java
字号:
package dbswing;
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 MultipleDataClassFrame2 extends JFrame implements ActionListener {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
Database database1 = new Database();
QueryDataSet queryDataSet1 = new QueryDataSet();
TableScrollPane tableScrollPane1 = new TableScrollPane();
JdbTable jdbTable1 = new JdbTable();
JPanel jPanel1 = new JPanel();
TableScrollPane tableScrollPane2 = new TableScrollPane();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JdbTree jdbTree1 = new JdbTree();
public MultipleDataClassFrame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("数据结构树和数据表格的实例");
//建立与SQLServer的books数据库的联接
database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor(
"jdbc:microsoft:sqlserver://bemyfriend:1433;DatabaseName=books", "sa",
"", false, "com.microsoft.jdbc.sqlserver.SQLServerDriver"));
//取得bookcategory数据表的记录
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(
database1, "select * from bookcategory", null, true, Load.ALL));
//设置按钮的属性
jButton1.setText("第一条");
jButton2.setText("上一条");
jButton3.setText("下一条");
jButton4.setText("最后一条");
jButton1.setActionCommand("first");
jButton2.setActionCommand("prior");
jButton3.setActionCommand("next");
jButton4.setActionCommand("last");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jButton4.addActionListener(this);
//设置表格的数据源
jdbTable1.setDataSet(queryDataSet1);
//创建一个根节点
DefaultMutableTreeNode top = new DefaultMutableTreeNode("树根");
//使用createNodes方法创建树的其它节点
createNodes(top);
//使用根节点创建树
jdbTree1 = new JdbTree(top);
//使根节点不可视
jdbTree1.setRootVisible(false);
//设置结构树的数据源和字段名
jdbTree1.setDataSet(queryDataSet1);
jdbTree1.setColumnName("categoryName");
jPanel1.add(tableScrollPane2, null);
jPanel2.add(jButton1, null);
jPanel2.add(jButton2, null);
jPanel2.add(jButton3, null);
jPanel2.add(jButton4, null);
tableScrollPane1.getViewport().add(jdbTable1, null);
tableScrollPane2.setPreferredSize(new Dimension(200, 100));
tableScrollPane2.getViewport().add(jdbTree1, null);
contentPane.add(tableScrollPane1, BorderLayout.CENTER);
contentPane.add(jPanel1, BorderLayout.NORTH);
contentPane.add(jPanel2, BorderLayout.SOUTH);
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void actionPerformed(ActionEvent e) {
String actionComm = e.getActionCommand();
if (actionComm.equals("first")) {
//显示第1条记录
queryDataSet1.first();
}
if (actionComm.equals("prior")) {
//显示上1条记录
queryDataSet1.prior();
}
if (actionComm.equals("next")) {
//显示下1条记录
queryDataSet1.next();
}
if (actionComm.equals("last")) {
//显示最后1条记录
queryDataSet1.last();
}
}
//创建树枝的方法,主要使用DefaultMutableTreeNode类
private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode category = null;
//树枝数组
String[][] categoryCaption = {
{"图书类别1", "图书类别1的描述"}
, {"图书类别2", "图书类别2的描述"}
, {"图书类别3", "图书类别3的描述"}
, {"图书类别4", "图书类别4的描述"}
, {"图书类别5", "图书类别5的描述"}
, {"图书类别6", "图书类别6的描述"}
, {"图书类别7", "图书类别7的描述"}
, {"图书类别8", "图书类别8的描述"}
, {"图书类别9", "图书类别9的描述"}
, {"图书类别10", "图书类别10的描述"}
, {"图书类别11", "图书类别11的描述"}
, {"图书类别12", "图书类别12的描述"}
, {"图书类别13", "图书类别13的描述"}
};
//创建树枝
for (int i = 0; i < categoryCaption.length; ++i) {
category = new DefaultMutableTreeNode(categoryCaption[i][0]);
category.add(new DefaultMutableTreeNode(categoryCaption[i][1]));
top.add(category);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -