📄 ttree.java
字号:
/* *TTree.java主要功能是建立左侧界面中的树状显示界面。 *该界面包括树状结构和查询按钮。 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.tree.*;
class Branch {
DefaultMutableTreeNode r;
public Branch(String[] data,int cnt,int start) {
r=new DefaultMutableTreeNode(data[start++]);
for(int i=0; i<cnt;i++)
r.add(new DefaultMutableTreeNode(data[start++]));
}
public DefaultMutableTreeNode node() {
return r;
}
}
public class TTree extends JPanel implements ActionListener,TreeSelectionListener {
TreeDataModel tdm;
int i=0;
int cnt;
DefaultMutableTreeNode root,child;
JTree tree;
public String condition;
public boolean isleaf;
public TTree() {
tdm = new TreeDataModel();
Container cp=this;
cp.setLayout(new BorderLayout());
root=new DefaultMutableTreeNode("类别");
tree=new JTree(root);
cp.add(new JScrollPane(tree),BorderLayout.CENTER);
tree.addTreeSelectionListener(this);
JButton fetch=new JButton("查询类别");
fetch.addActionListener(this);
JPanel p=new JPanel();
p.add(fetch);
cp.add(p,BorderLayout.SOUTH);
cp.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int i=0;
int j=0;
int count=0;
cnt = tdm.selCateCnt();
int [] scnt = new int[cnt];
tdm.selSubCateCnt(scnt);
for (i=0;i<cnt;i++)
count+=scnt[i]+1;
String [] data = new String[count];
tdm.selCate(data,scnt,cnt);
i=0;
j=0;
root.removeAllChildren();
while (i<cnt) {
child=new Branch(data,scnt[i],j).node();
j+=scnt[i]+1;
i++;
root.add(child);
}
}
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject();
condition = nodeInfo.toString();
isleaf = node.isLeaf();
Share.condition = condition;
Share.isleaf = isleaf;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -