当前位置: 技术问答>java相关
请教各位java高手!
来源: 互联网 发布时间:2015-05-15
本文导语: 我想用java做一个类似于蜘蛛程序的java程序来实现对网页超级链接的遍历,并将网页超级链接结构用树的形式表现出来,如何能实现。谢谢! | import javax.swing.*; import javax.swing.tree.TreePath; import...
我想用java做一个类似于蜘蛛程序的java程序来实现对网页超级链接的遍历,并将网页超级链接结构用树的形式表现出来,如何能实现。谢谢!
|
import javax.swing.*;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.tree.*;
import java.applet.AppletContext;
import java.net.URL;
import java.applet.Applet;
import java.net.MalformedURLException;
public class MyApplet extends JApplet{
public void init(){
TreeModel tm = makeTree();
JTree mytree = new JTree(tm);
getContentPane().add(new JScrollPane(mytree));
mytree.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
URL url = null;
URL fullurl = null;
url = getDocumentBase();
JTree t = (JTree)e.getSource();
int row = t.getRowForLocation(e.getX(),e.getY());
AppletContext ac = getAppletContext();
if(row != -1){
TreePath path = t.getPathForRow(row);
TreeNode node = (TreeNode)path.getLastPathComponent();
if((node.toString()).equals("数控车削中心")){
try{
fullurl = new URL(url,"jiagzx.jsp");
}catch(MalformedURLException ev)
{
showStatus("Error"+e.toString());
}
ac.showDocument(fullurl,"rightframe");
}
else if((node.toString()).equals("生产现场一")){
try{
fullurl = new URL(url,"1.htm");
}catch(MalformedURLException ev)
{
showStatus("Error"+e.toString());
}
ac.showDocument(fullurl,"rightframe");
}
else{
try{
fullurl = new URL(url,"daijianshe.jsp");
}catch(MalformedURLException ev){
showStatus("Error"+e.toString());
}
ac.showDocument(fullurl,"rightframe");
}
}
}
});
}
protected static TreeModel makeTree(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode("系统");
DefaultMutableTreeNode parent;
//一车间节点图
parent = new DefaultMutableTreeNode("生产现场一");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
//二车间节点图
parent = new DefaultMutableTreeNode("生产现场二");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
//三车间节点图
parent = new DefaultMutableTreeNode("生产现场三");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
//四车间节点图
parent = new DefaultMutableTreeNode("生产现场四");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
return new DefaultTreeModel(root);
}
}
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.tree.*;
import java.applet.AppletContext;
import java.net.URL;
import java.applet.Applet;
import java.net.MalformedURLException;
public class MyApplet extends JApplet{
public void init(){
TreeModel tm = makeTree();
JTree mytree = new JTree(tm);
getContentPane().add(new JScrollPane(mytree));
mytree.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
URL url = null;
URL fullurl = null;
url = getDocumentBase();
JTree t = (JTree)e.getSource();
int row = t.getRowForLocation(e.getX(),e.getY());
AppletContext ac = getAppletContext();
if(row != -1){
TreePath path = t.getPathForRow(row);
TreeNode node = (TreeNode)path.getLastPathComponent();
if((node.toString()).equals("数控车削中心")){
try{
fullurl = new URL(url,"jiagzx.jsp");
}catch(MalformedURLException ev)
{
showStatus("Error"+e.toString());
}
ac.showDocument(fullurl,"rightframe");
}
else if((node.toString()).equals("生产现场一")){
try{
fullurl = new URL(url,"1.htm");
}catch(MalformedURLException ev)
{
showStatus("Error"+e.toString());
}
ac.showDocument(fullurl,"rightframe");
}
else{
try{
fullurl = new URL(url,"daijianshe.jsp");
}catch(MalformedURLException ev){
showStatus("Error"+e.toString());
}
ac.showDocument(fullurl,"rightframe");
}
}
}
});
}
protected static TreeModel makeTree(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode("系统");
DefaultMutableTreeNode parent;
//一车间节点图
parent = new DefaultMutableTreeNode("生产现场一");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
//二车间节点图
parent = new DefaultMutableTreeNode("生产现场二");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
//三车间节点图
parent = new DefaultMutableTreeNode("生产现场三");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
//四车间节点图
parent = new DefaultMutableTreeNode("生产现场四");
root.add(parent);
parent.add(new DefaultMutableTreeNode("数控车床"));
parent.add(new DefaultMutableTreeNode("数控专用铣床"));
parent.add(new DefaultMutableTreeNode("数控升降台铣床"));
parent.add(new DefaultMutableTreeNode("龙门加工中心"));
parent.add(new DefaultMutableTreeNode("立式钻床"));
parent.add(new DefaultMutableTreeNode("数控车削中心"));
return new DefaultTreeModel(root);
}
}