⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydesktop.java

📁 用applet生成网页。直接在applet上拖动鼠标
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package mydesktop;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.util.*;import java.io.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2006</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class MyDeskTop extends Applet {  private boolean isStandalone = false;  BorderLayout borderLayout1 = new BorderLayout();  JPanel DeskTopPane = new JPanel();  JPanel toolPane = new JPanel();  JButton butPromulgate = new JButton("发布网页");  JComboBox jcmb = new JComboBox(new String[]{"文本","超级连接","图片"});  Rect rect=new Rect();  Vector vetText=new Vector();  textMouseAdapter textAdapter=new textMouseAdapter(this);  textMouseMotionAdapter textMotionAdapter=new textMouseMotionAdapter(this);  int startx=0;  int starty=0;  int potW=8;  int potH=8;  int potx=0,poty=0;  JComponent selectedObj=null;  JComponent jcomp = null;  JPanel potRB=new JPanel();  JPanel potRB1=new JPanel();//左上角  JPanel potRB2=new JPanel();//左下角  JPanel potRB3=new JPanel();//左中  JPanel potRB4=new JPanel();//右上角  JPanel potRB5=new JPanel();//右中  JPanel potRB6=new JPanel();//上中  JPanel potRB7=new JPanel();//下中  //Get a parameter value  public String getParameter(String key, String def) {    return isStandalone ? System.getProperty(key, def) :      (getParameter(key) != null ? getParameter(key) : def);  }  //Construct the applet  public MyDeskTop() {  }  //Initialize the applet  public void init() {    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception {    this.potRB.setBackground(Color.black);    this.potRB.setBounds(0,0,potW,potH);    potRB.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB));    potRB.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB));    this.potRB.setVisible(false);    this.DeskTopPane.add(potRB,null);        //左上角    this.potRB1.setBackground(Color.black);    this.potRB1.setBounds(0,0,potW,potH);    potRB1.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB1));    potRB1.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB1));    this.potRB1.setVisible(false);    this.DeskTopPane.add(potRB1,null);    //左下角    this.potRB2.setBackground(Color.black);    this.potRB2.setBounds(0,0,potW,potH);    potRB2.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB2));    potRB2.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB2));    this.potRB2.setVisible(false);    this.DeskTopPane.add(potRB2,null);    //    this.potRB3.setBackground(Color.black);    this.potRB3.setBounds(0,0,potW,potH);    potRB3.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB3));    potRB3.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB3));    this.potRB3.setVisible(false);    this.DeskTopPane.add(potRB3,null);    //    this.potRB4.setBackground(Color.black);    this.potRB4.setBounds(0,0,potW,potH);    potRB4.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB4));    potRB4.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB4));    this.potRB4.setVisible(false);    this.DeskTopPane.add(potRB4,null);    //    this.potRB5.setBackground(Color.black);    this.potRB5.setBounds(0,0,potW,potH);    potRB5.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB5));    potRB5.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB5));    this.potRB5.setVisible(false);    this.DeskTopPane.add(potRB5,null);    //    this.potRB6.setBackground(Color.black);    this.potRB6.setBounds(0,0,potW,potH);    potRB6.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB6));    potRB6.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB6));    this.potRB6.setVisible(false);    this.DeskTopPane.add(potRB6,null);    //    this.potRB7.setBackground(Color.black);    this.potRB7.setBounds(0,0,potW,potH);    potRB7.addMouseListener(new MyDeskTop_potRB_mouseAdapter(this,potRB7));    potRB7.addMouseMotionListener(new MyDeskTop_potRB_mouseMotionAdapter(this,potRB7));    this.potRB7.setVisible(false);    this.DeskTopPane.add(potRB7,null);        this.setLayout(borderLayout1);    DeskTopPane.setBackground(Color.pink);    DeskTopPane.addMouseMotionListener(new MyDeskTop_DeskTopPane_mouseMotionAdapter(this));    DeskTopPane.addMouseListener(new MyDeskTop_DeskTopPane_mouseAdapter(this));    DeskTopPane.setLayout(null);    rect.setDoubleBuffered(false);    this.add(DeskTopPane,  BorderLayout.CENTER);    this.add(toolPane,BorderLayout.SOUTH);    toolPane.add(jcmb);    toolPane.add(butPromulgate);    butPromulgate.addActionListener(new ActionListener(){    	public void actionPerformed(ActionEvent ae){    		String content="";    		for(int i=0;i<vetText.size();i++){    			if(vetText.elementAt(i) instanceof MyLink){    				content+="<table border=0 style='position:absolute;left:"+((MyLink)vetText.elementAt(i)).getX()+";top:"+((MyLink)vetText.elementAt(i)).getY()+";width:"+((MyLink)vetText.elementAt(i)).getWidth()+";height:"+((MyLink)vetText.elementAt(i)).getHeight()+"'>";    				content+="<tr><td>";    				content+="<a href='"+((MyLink)vetText.elementAt(i)).getUrlText()+"'>"+((MyLink)vetText.elementAt(i)).getText()+"</a>";    				content+="<td></tr></table>";    			}    			else if(vetText.elementAt(i) instanceof MyPicture){    				content+="<table border=0 style='position:absolution;left:"+((MyPicture)vetText.elementAt(i)).getX()+";top:"+((MyPicture)vetText.elementAt(i)).getY()+";width:"+((MyPicture)vetText.elementAt(i)).getWidth()+";height:"+((MyPicture)vetText.elementAt(i)).getHeight()+"'>";    				content+="<tr><td><img src='";    				content+=((MyPicture)vetText.elementAt(i)).getPicture();    				content+="'><td></tr></table>";    			}    			else if(vetText.elementAt(i) instanceof JLabel){    				content+="<table border=0 style='position:absolution;left:"+((JLabel)vetText.elementAt(i)).getX()+";top:"+((JLabel)vetText.elementAt(i)).getY()+";width:"+((JLabel)vetText.elementAt(i)).getWidth()+";height:"+((JLabel)vetText.elementAt(i)).getHeight()+"'>";    				content+="<tr><td>";    				content+=((JLabel)vetText.elementAt(i)).getText();    				content+="<td></tr></table>";    			}    			    		}    		File file=new File("G:/java/WorkSpace/MyDesktop/mydesktop/desktop.html");    		if(file.exists()){    			file.delete();    		}    		try{    			file.createNewFile();    			FileOutputStream out = new FileOutputStream(file);    			out.write(content.getBytes());    			out.flush();    			out.close();    		}    		catch(Exception e){    			e.printStackTrace();    		}    	}    });    rect.setBounds(new Rectangle(152, 46, 100, 100));    DeskTopPane.add(rect, null);    rect.setVisible(false);  }  //Get Applet information  public String getAppletInfo() {    return "Applet Information";  }  //Get parameter info  public String[][] getParameterInfo() {    return null;  }  void DeskTopPane_mousePressed(MouseEvent e) {//按下容器     rect.setVisible(true);     rect.setLocation(e.getX(),e.getY());     rect.setSize(0,0);     this.potRB.setVisible(false);     this.potRB1.setVisible(false);     this.potRB2.setVisible(false);     this.potRB3.setVisible(false);     this.potRB4.setVisible(false);     this.potRB5.setVisible(false);     this.potRB6.setVisible(false);     this.potRB7.setVisible(false);     selectedObj=null;  }  void DeskTopPane_mouseDragged(MouseEvent e) {	  rect.setSize(e.getX()-rect.getX(),e.getY()-rect.getY());  }  void DeskTopPane_mouseReleased(MouseEvent e) {     //鼠标释放	  MyLink myLink = new MyLink();      JLabel jlb=new JLabel();      MyPicture picture = new MyPicture();	  if(this.jcmb.getSelectedIndex() == 1){//超连接		  jcomp = myLink;		  rect.setVisible(false);		  myLink.setBounds(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight());	      this.DeskTopPane.add(myLink);	      myLink.setBorder(BorderFactory.createLineBorder(Color.black));//设置边框	      myLink.setText("文本对象");	      myLink.addMouseListener(this.textAdapter);	      myLink.addMouseMotionListener(this.textMotionAdapter);	      vetText.addElement(myLink);	  }	  else if(this.jcmb.getSelectedIndex() == 0){//文本		  jcomp = jlb;		  rect.setVisible(false);	      jlb.setBounds(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight());	      this.DeskTopPane.add(jlb);	      jlb.setBorder(BorderFactory.createLineBorder(Color.black));//设置边框	      jlb.setText("文本对象");	      jlb.addMouseListener(this.textAdapter);	      jlb.addMouseMotionListener(this.textMotionAdapter);	      vetText.addElement(jlb);	  }	  else if(this.jcmb.getSelectedIndex() == 2){//图片		  jcomp = picture;		  rect.setVisible(false);		  picture.setBounds(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight());	      this.DeskTopPane.add(picture);	      picture.setBorder(BorderFactory.createLineBorder(Color.black));//设置边框	      picture.setPicture("images/1.gif");	      picture.addMouseListener(this.textAdapter);	      picture.addMouseMotionListener(this.textMotionAdapter);	      vetText.addElement(picture);	  }        }  void textPressed(MouseEvent e){//文本按下//    System.out.println("文本按下"+e.getX());    startx=e.getX();    starty=e.getY();	JComponent jComponent ;	jComponent=(JComponent)e.getSource();//取得按下的文本对象    this.potRB.setVisible(true);    this.potRB.setLocation(jComponent.getX()+jComponent.getWidth()-potRB.getWidth()/2,                           jComponent.getY()+jComponent.getHeight()-potRB.getHeight()/2);    this.potRB1.setVisible(true);    this.potRB1.setLocation(jComponent.getX()-potRB1.getWidth()/2,                           jComponent.getY()-potRB1.getHeight()/2);    this.potRB2.setVisible(true);    this.potRB2.setLocation(jComponent.getX()-potRB2.getWidth()/2,                           jComponent.getY()+jComponent.getHeight()-potRB2.getHeight()/2);    this.potRB3.setVisible(true);    this.potRB3.setLocation(jComponent.getX()-potRB3.getWidth()/2,                           jComponent.getY()+jComponent.getHeight()/2-potRB3.getHeight()/2);    this.potRB4.setVisible(true);    this.potRB4.setLocation(jComponent.getX()+jComponent.getWidth()-potRB4.getWidth()/2,                           jComponent.getY()-potRB4.getHeight()/2);    this.potRB5.setVisible(true);    this.potRB5.setLocation(jComponent.getX()+jComponent.getWidth()-potRB5.getWidth()/2,                           jComponent.getY()+jComponent.getHeight()/2-potRB5.getHeight()/2);    this.potRB6.setVisible(true);    this.potRB6.setLocation(jComponent.getX()+jComponent.getWidth()/2-potRB6.getWidth()/2,                           jComponent.getY()-potRB6.getHeight()/2);    this.potRB7.setVisible(true);    this.potRB7.setLocation(jComponent.getX()+jComponent.getWidth()/2-potRB7.getWidth()/2,                           jComponent.getY()+jComponent.getHeight()-potRB7.getHeight()/2);    this.selectedObj=jComponent;  }  void textMouseClicked(MouseEvent e){     if(e.getClickCount()==2){    	 if(selectedObj instanceof MyLink){    		 MyLinkDialog text=new MyLinkDialog(null,"文本内容",true);    	        text.setText(((MyLink)selectedObj).getText());    	        text.setUrlText(((MyLink)selectedObj).getUrlText());    	        text.setBounds(0,0,300,300);    	        text.setVisible(true);    	        String result=text.getText();    	        ((MyLink)selectedObj).setText(result);     	        ((MyLink)selectedObj).setUrlText(text.getUrlText());    	 }    	 else if(selectedObj instanceof MyPicture){    		 MyPictureDialog pictureDialog=new MyPictureDialog(null,"图片",true);    		 pictureDialog.setPicture(((MyPicture)selectedObj).getPicture());    		 pictureDialog.setBounds(0,0,300,300);    		 pictureDialog.setVisible(true); 	        String result=pictureDialog.urlText.getText(); 	        ((MyPicture)selectedObj).setPicture(result);    	 }    	 else if(selectedObj instanceof JLabel){    		 TDialog text=new TDialog(null,"文本内容",true);    	        text.setText(((JLabel)selectedObj).getText());    	        text.setBounds(0,0,300,300);    	        text.setVisible(true);    	        String result=text.textArea.getText();    	        ((JLabel)selectedObj).setText(result);    	 }    	             }  }  void textMouseReleased(MouseEvent e){//文本释放    //System.out.println("文本释放");  }  void textMouseDragged(MouseEvent e){//文本拖动      //System.out.println("文本拖动");	  int width,height;      JLabel jlb=(JLabel)e.getSource();      width = jlb.getX()+(e.getX()-startx);      height = jlb.getY()+(e.getY()-starty);      if(jlb.getX()<this.DeskTopPane.getX()){    	  width = this.DeskTopPane.getX();      }      if(jlb.getY()<DeskTopPane.getY()){    	  height = DeskTopPane.getY();      }      if(jlb.getX()+jlb.getWidth()>DeskTopPane.getX()+DeskTopPane.getWidth()){    	  width = DeskTopPane.getX()+DeskTopPane.getWidth()-jlb.getWidth();      }      if(jlb.getY()+jlb.getHeight()>DeskTopPane.getY()+DeskTopPane.getHeight()){    	 height = DeskTopPane.getY()+DeskTopPane.getHeight()-jlb.getHeight();      }      jlb.setLocation(width,height);      //让点跟着动      potFollow(jlb);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -