当前位置:编程学习 > JAVA >>

用Java写一个在线金山词霸程序

代码贴上, 其它的就不多说了
 
import java.awt.AWTEvent;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
 
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
 
import com.sun.awt.AWTUtilities;
 
class RefBoolean {public boolean var = false;}
class RefInt {public int var = 0;}
 
public class DictFrame { 
 public static void main(String[] args) {
  try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");}
  catch (Exception e) { System.out.println(e); }
  
  Toolkit tk = Toolkit.getDefaultToolkit();
  
  final RefBoolean canDrag = new RefBoolean();
  final RefBoolean isFirst = new RefBoolean();
  final RefInt oldX = new RefInt();
  final RefInt oldY = new RefInt();
  
  isFirst.var = true;
  
  final JFrame jf = new JFrame();
  final JDialog jdmean = new JDialog();
  final JTextField jtfWord = new JTextField("");
  final JTextArea jtamean = new JTextArea("");
  JScrollPane jScmean=new JScrollPane(jtamean);
  JPanel jp = new JPanel();
  
  Image image = tk.getImage("c:/ico.jpg"); //一张16*16的图片
  
  jf.setAlwaysOnTop(true);
  jf.setIconImage(image);
  
  jdmean.setBounds((int)(tk.getScreenSize().getWidth()-270), (int)(tk.getScreenSize().getHeight()-320)+60, 260, 200);
  jdmean.setUndecorated(true);
  jdmean.setAlwaysOnTop(true);
  jtamean.setLineWrap(true);
  jdmean.add(jScmean);
  jdmean.setVisible(true);
  
  AWTUtilities.setWindowOpacity(jf,0.9f);
  AWTUtilities.setWindowOpacity(jdmean,0.0f);
  
  jtfWord.setBounds(5, 10, 250, 21);
  jp.setLayout(null);
  jp.add(jtfWord);
  
  jp.setBackground(Color.GRAY);
  jf.setContentPane(jp);
 
  jf.setBounds((int)(tk.getScreenSize().getWidth()-270), (int)(tk.getScreenSize().getHeight()-320), 260, 50);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setUndecorated(true);
  
  jp.validate();
  jf.setVisible(true);
  
  try {
   if(SystemTray.isSupported()) {
    SystemTray st = SystemTray.getSystemTray();
    TrayIcon ti = new TrayIcon(image);
    ti.setToolTip("dict");
    
    PopupMenu popupMenu1 = new PopupMenu();  
    MenuItem menuItem1 = new MenuItem(); 
 
    ti.setPopupMenu(popupMenu1);
    
    ti.addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent e) { 
      if (e.getClickCount() == 2) { 
       jf.setVisible(true);
      }
     }
    });
 
    st.add(ti);
    
    popupMenu1.setLabel("dict");
    menuItem1.setLabel("退出");
    menuItem1.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
      System.exit(0);
     }
    });
    popupMenu1.add(menuItem1);
   }
  } catch (AWTException e) {
   e.printStackTrace();
  }
  jf.addWindowListener(new WindowAdapter() {
   public void windowIconified(WindowEvent evt) {
   }
  });
  
  tk.addAWTEventListener(new AWTEventListener() {
   @Override
   public void eventDispatched(AWTEvent event) {
    if(((KeyEvent) event).getID() == KeyEvent.KEY_PRESSED) {
     if(((KeyEvent) event).getKeyCode()==KeyEvent.VK_F4) {
      System.exit(0);
     }else if( ((KeyEvent) event).getKeyCode()==KeyEvent.VK_ESCAPE) {
      jf.setVisible(false);
      
      if(AWTUtilities.getWindowOpacity(jdmean)>0.1d) {
       double[] ds = {0.69, 0.59, 0.49, 0.45, 0.39, 0.35, 0.29, 0.25, 0.19, 0.1, 0.05, 0.0};
       for(double d:ds) {
        _sleep(10);
        AWTUtilities.setWindowOpacity(jdmean,(float)d);
       }
      }
     }  else if( ((KeyEvent) event).getKeyCode()==KeyEvent.VK_F1) {
      jf.setVisible(true);
      jtfWord.requestFocus();
     }  
    }
   }
  }, AWTEvent.KEY_EVENT_MASK);
 
  jp.addMouseMotionListener(new MouseMotionListener() {
   @Override
   public void mouseMoved(MouseEvent arg0) {    
   }
   
   @Override
   public void mouseDragged(MouseEvent arg0) {
    int x = arg0.getX() + jf.getX();
    int y = arg0.getY() + jf.getY();
    
    if(isFirst.var) {
     oldX.var = x;
     oldY.var = y;
     isFirst.var = false;
    }
 
    int detaX = x-oldX.var;
    int detaY = y-oldY.var;
    
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,