源代码如下:
public class Frame1 extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JTree jTree1 = new JTree();
private JButton jButton1 = new JButton();
private JPopupMenu jPopupMenu1 = new JPopupMenu();
private JMenu jMenu1 = new JMenu();
private JMenu jMenu2 = new JMenu();
private JMenuItem jMenuItem1 = new JMenuItem();
private JMenuItem jMenuItem2 = new JMenuItem();
private JMenuItem jMenuItem3 = new JMenuItem(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.addKeyListener(new java.awt.event.KeyAdapter() { }); jPanel1.setLayout(xYLayout1);
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setToolTipText("");
jPanel1.setToolTipText("");
jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
//public void mouseClicked(MouseEvent e) {
//jPanel1_mouseClicked(e);
//}
});
jPopupMenu1.setInvoker(this);
jPopupMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jPopupMenu1_mouseClicked(e);
}
});
jMenu1.setText("open");
jMenuItem1.setText("hoio");
jMenu2.setText("sfskl");
jMenuItem2.setText("fenjierw");
jMenuItem3.setText("sfbuiea");
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTree1, new XYConstraints(20, 16, 309, 224));
jPanel1.add(jButton1, new XYConstraints(196, 259, 81, 30));
jPopupMenu1.add(jMenu1);
jMenu1.add(jMenu2);
jMenu1.add(jMenuItem1);
jMenu2.add(jMenuItem2);
jMenu2.add(jMenuItem3);
jTree1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent me){
jPopupMenu1_mouseClicked(me);
}
}); }
//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);
}
} void jButton1_actionPerformed(ActionEvent e) {
class Tdata /*二叉树的定义*/
{
int data;
Tdata child,brother;
}
class TreeLink /*工作链表的定义*/
{
Tdata Node;
DefaultMutableTreeNode a; /*定义节点a*/
TreeLink next; /*链指针*/
TreeLink()
{
Node=null;
next=null;
}
}
/*程序框架*/
TreeLink head,p;
Tdata data=new Tdata();
Tdata data1=new Tdata();
Tdata data2=new Tdata();
Tdata data3=new Tdata();
Tdata data4=new Tdata();
Tdata data5=new Tdata();
Tdata data6=new Tdata();
Tdata data7=new Tdata();
data.data=0;
data1.data=1;
data2.data=2;
data3.data=3;
data4.data=4;
data5.data=5;
data6.data=6;
data7.data=7;
//data1、data2、data3属于第一级
data1.child=data.child;
data.child=data1;
data2.brother=data1.brother;
data1.brother=data2;
data3.brother=data2.child;
data2.brother=data3;
//data4、data5是data2data2的child
data4.child=data2.child;
data2.child=data4;
data5.brother=data4.brother;
data4.brother=data5;
//data6是data3的child
data6.child=data3.child;
data3.child=data6;
//data7是data5的chlid
data7.child=data5.child;
data5.child=data7;
//生成只有child的单级树
/*int i;
int j=1;
for(i=j;i<10;i++){ data1=new Tdata();
data2=new Tdata();
data1.data=j;
data2.data=j+1;
data1.child=data.child;
data.child=data1;
data2.brother=data1.brother;
data1.brother=data2; j++;
}*/
head=new TreeLink(); /*实例化*/
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultTreeModel mod;
p=new TreeLink(); /*实例化*/
p.Node=data;
p.a=root; /*储存"root",用以实现二叉树和jtree的一一对应*/
p.next=head.next; /*将root点加入链表的指针变化*/
head.next=p; /*将root点加入链表的指针变化*/
while(head.next!=null){
p=head.next; /*取链表的头节点*/
head.next=p.next; /*取链表的头节点*/
Tdata node=p.Node;
node=node.child;
while(node!=null){
DefaultMutableTreeNode no=new DefaultMutableTreeNode(String.valueOf(node.data));
/*将no以文件夹的形式画在root的下一级*/
p.a.add(no);
TreeLink tt=new TreeLink();
tt.Node=node;
tt.a=no;
tt.next=head.next;
head.next=tt;
node=node.brother;
}
}
mod=new DefaultTreeModel(root);
jTree1.setModel(mod);
} void jPopupMenu1_mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
jPopupMenu1.show(this,e.getX(),e.getY());
}
}
}
public class Frame1 extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JTree jTree1 = new JTree();
private JButton jButton1 = new JButton();
private JPopupMenu jPopupMenu1 = new JPopupMenu();
private JMenu jMenu1 = new JMenu();
private JMenu jMenu2 = new JMenu();
private JMenuItem jMenuItem1 = new JMenuItem();
private JMenuItem jMenuItem2 = new JMenuItem();
private JMenuItem jMenuItem3 = new JMenuItem(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.addKeyListener(new java.awt.event.KeyAdapter() { }); jPanel1.setLayout(xYLayout1);
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setToolTipText("");
jPanel1.setToolTipText("");
jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
//public void mouseClicked(MouseEvent e) {
//jPanel1_mouseClicked(e);
//}
});
jPopupMenu1.setInvoker(this);
jPopupMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jPopupMenu1_mouseClicked(e);
}
});
jMenu1.setText("open");
jMenuItem1.setText("hoio");
jMenu2.setText("sfskl");
jMenuItem2.setText("fenjierw");
jMenuItem3.setText("sfbuiea");
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTree1, new XYConstraints(20, 16, 309, 224));
jPanel1.add(jButton1, new XYConstraints(196, 259, 81, 30));
jPopupMenu1.add(jMenu1);
jMenu1.add(jMenu2);
jMenu1.add(jMenuItem1);
jMenu2.add(jMenuItem2);
jMenu2.add(jMenuItem3);
jTree1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent me){
jPopupMenu1_mouseClicked(me);
}
}); }
//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);
}
} void jButton1_actionPerformed(ActionEvent e) {
class Tdata /*二叉树的定义*/
{
int data;
Tdata child,brother;
}
class TreeLink /*工作链表的定义*/
{
Tdata Node;
DefaultMutableTreeNode a; /*定义节点a*/
TreeLink next; /*链指针*/
TreeLink()
{
Node=null;
next=null;
}
}
/*程序框架*/
TreeLink head,p;
Tdata data=new Tdata();
Tdata data1=new Tdata();
Tdata data2=new Tdata();
Tdata data3=new Tdata();
Tdata data4=new Tdata();
Tdata data5=new Tdata();
Tdata data6=new Tdata();
Tdata data7=new Tdata();
data.data=0;
data1.data=1;
data2.data=2;
data3.data=3;
data4.data=4;
data5.data=5;
data6.data=6;
data7.data=7;
//data1、data2、data3属于第一级
data1.child=data.child;
data.child=data1;
data2.brother=data1.brother;
data1.brother=data2;
data3.brother=data2.child;
data2.brother=data3;
//data4、data5是data2data2的child
data4.child=data2.child;
data2.child=data4;
data5.brother=data4.brother;
data4.brother=data5;
//data6是data3的child
data6.child=data3.child;
data3.child=data6;
//data7是data5的chlid
data7.child=data5.child;
data5.child=data7;
//生成只有child的单级树
/*int i;
int j=1;
for(i=j;i<10;i++){ data1=new Tdata();
data2=new Tdata();
data1.data=j;
data2.data=j+1;
data1.child=data.child;
data.child=data1;
data2.brother=data1.brother;
data1.brother=data2; j++;
}*/
head=new TreeLink(); /*实例化*/
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultTreeModel mod;
p=new TreeLink(); /*实例化*/
p.Node=data;
p.a=root; /*储存"root",用以实现二叉树和jtree的一一对应*/
p.next=head.next; /*将root点加入链表的指针变化*/
head.next=p; /*将root点加入链表的指针变化*/
while(head.next!=null){
p=head.next; /*取链表的头节点*/
head.next=p.next; /*取链表的头节点*/
Tdata node=p.Node;
node=node.child;
while(node!=null){
DefaultMutableTreeNode no=new DefaultMutableTreeNode(String.valueOf(node.data));
/*将no以文件夹的形式画在root的下一级*/
p.a.add(no);
TreeLink tt=new TreeLink();
tt.Node=node;
tt.a=no;
tt.next=head.next;
head.next=tt;
node=node.brother;
}
}
mod=new DefaultTreeModel(root);
jTree1.setModel(mod);
} void jPopupMenu1_mouseClicked(MouseEvent e) {
if(e.getModifiers()==Event.META_MASK){
jPopupMenu1.show(this,e.getX(),e.getY());
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货