熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用Java寫一個在線金山詞霸程序

2013-11-23 18:52:16  來源: Java核心技術 

  代碼貼上 其它的就不多說了

  import javaawtAWTEvent;

  import javaawtAWTException;

  import javaawtColor;

  import javaawtImage;

  import javaawtMenuItem;

  import javaawtPopupMenu;

  import javaawtSystemTray;

  import javaawtToolkit;

  import javaawtTrayIcon;

  import javaawteventAWTEventListener;

  import javaawteventActionEvent;

  import javaawteventActionListener;

  import javaawteventKeyEvent;

  import javaawteventKeyListener;

  import javaawteventMouseAdapter;

  import javaawteventMouseEvent;

  import javaawteventMouseListener;

  import javaawteventMouseMotionListener;

  import javaawteventWindowAdapter;

  import javaawteventWindowEvent;

  import javaioInputStream;

  import URL;

  import javautilArrays;

  import javaxswingJDialog;

  import javaxswingJFrame;

  import javaxswingJPanel;

  import javaxswingJScrollPane;

  import javaxswingJTextArea;

  import javaxswingJTextField;

  import javaxswingUIManager;

  import comsunawtAWTUtilities;

  class RefBoolean {public boolean var = false;}

  class RefInt {public int var = ;}

  public class DictFrame {

  public static void main(String[] args) {

  try {UIManagersetLookAndFeel(comsunjavaswingplafwindowsWindowsLookAndFeel}

  catch (Exception e) { Systemoutprintln(e) }

  Toolkit tk = ToolkitgetDefaultToolkit()

  final RefBoolean canDrag = new RefBoolean()

  final RefBoolean isFirst = new RefBoolean()

  final RefInt oldX = new RefInt()

  final RefInt oldY = new RefInt()

  isFirstvar = 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 = tkgetImage(c:/icojpg //一張*的圖片

  jfsetAlwaysOnTop(true)

  jfsetIconImage(image)

  jdmeansetBounds((int)(tkgetScreenSize()getWidth() (int)(tkgetScreenSize()getHeight())+

  jdmeansetUndecorated(true)

  jdmeansetAlwaysOnTop(true)

  jtameansetLineWrap(true)

  jdmeanadd(jScmean)

  jdmeansetVisible(true)

  AWTUtilitiessetWindowOpacity(jff)

  AWTUtilitiessetWindowOpacity(jdmeanf)

  jtfWordsetBounds(

  jpsetLayout(null)

  jpadd(jtfWord)

  jpsetBackground(ColorGRAY)

  jfsetContentPane(jp)

  jfsetBounds((int)(tkgetScreenSize()getWidth() (int)(tkgetScreenSize()getHeight()

  jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE)

  jfsetUndecorated(true)

  jpvalidate()

  jfsetVisible(true)

  try {

  if(SystemTrayisSupported()) {

  SystemTray st = SystemTraygetSystemTray()

  TrayIcon ti = new TrayIcon(image)

  tisetToolTip(dict

  PopupMenu popupMenu = new PopupMenu()

  MenuItem menuItem = new MenuItem()

  tisetPopupMenu(popupMenu

  tiaddMouseListener(new MouseAdapter() {

  public void mouseClicked(MouseEvent e) {

  if (egetClickCount() == ) {

  jfsetVisible(true)

  }

  }

  })

  stadd(ti)

  popupMenusetLabel(dict

  menuItemsetLabel(退出

  menuItemaddActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent evt) {

  Systemexit(

  }

  })

  popupMenuadd(menuItem

  }

  } catch (AWTException e) {

  eprintStackTrace()

  }

  jfaddWindowListener(new WindowAdapter() {

  public void windowIconified(WindowEvent evt) {

  }

  })

  tkaddAWTEventListener(new AWTEventListener() {

  @Override

  public void eventDispatched(AWTEvent event) {

  if(((KeyEvent) event)getID() == KeyEventKEY_PRESSED) {

  if(((KeyEvent) event)getKeyCode()==KeyEventVK_F) {

  Systemexit(

  }else if( ((KeyEvent) event)getKeyCode()==KeyEventVK_ESCAPE) {

  jfsetVisible(false)

  if(AWTUtilitiesgetWindowOpacity(jdmean)>d) {

  double[] ds = { };

  for(double d:ds) {

  _sleep(

  AWTUtilitiessetWindowOpacity(jdmean(float)d)

  }

  }

  }  else if( ((KeyEvent) event)getKeyCode()==KeyEventVK_F) {

  jfsetVisible(true)

  jtfWordrequestFocus()

  }

  }

  }

  } AWTEventKEY_EVENT_MASK)

  jpaddMouseMotionListener(new MouseMotionListener() {

  @Override

  public void mouseMoved(MouseEvent arg) {

  }

  @Override

  public void mouseDragged(MouseEvent arg) {

  int x = arggetX() + jfgetX()

  int y = arggetY() + jfgetY()

  if(isFirstvar) {

  oldXvar = x;

  oldYvar = y;

  isFirstvar = false;

  }

  int detaX = xoldXvar;

  int detaY = yoldYvar;

  jfsetBounds(jfgetX()+detaX jfgetY()+detaY

  jdmeansetBounds(jfgetX()+detaX jfgetY()+detaY+

  oldXvar = x;

  oldYvar = y;

  }

  })

  jpaddMouseListener(new MouseListener() {

  @Override

  public void mouseReleased(MouseEvent arg) {

  canDragvar = false;

  isFirstvar = false;

  }

  @Override

  public void mousePressed(MouseEvent arg) {

  canDragvar = true;

  isFirstvar = true;

  }

  @Override

  public void mouseExited(MouseEvent arg) {

  }

  @Override

  public void mouseEntered(MouseEvent arg) {

  }

  @Override

  public void mouseClicked(MouseEvent arg) {

  }

  })

  jfaddWindowListener(new WindowAdapter() {

  public void windowIconified(WindowEvent evt) {

  AWTUtilitiessetWindowOpacity(jdmeanf)

  }

  })

  jtfWordaddKeyListener(new KeyListener() {

  @Override

  public void keyTyped(KeyEvent e) {

  }

  @Override

  public void keyReleased(KeyEvent e) {

  if( egetKeyCode()==KeyEventVK_ENTER) {

  if((egetModifiersEx() & KeyEventSHIFT_DOWN_MASK) != ) {

  jtfWordsetText(

  jtameansetText(

  Thread t = new Thread(new Runnable() {

  @Override

  public void run() {

  if(AWTUtilitiesgetWindowOpacity(jdmean)>d) {

  double[] ds = { };

  for(int i=; i<dslength; i++) {

  double d = ds[i];

  _sleep(

  AWTUtilitiessetWindowOpacity(jdmean(float)d)

  jdmeansetBounds(jdmeangetX() jdmeangetY() (int)((*i)* (int)((*i)*))

  }

  }

  }

  })

  tstart()

  return;

  }

  try {

  String url = dictcoiciba/api/dictionaryphp?key={這裡需要填Key你可以到金山詞霸Api官網審請審請到key後把這段廢話去掉左右大括號也要去掉然後填入你的key本人有一個key但不便放在網上共享}&w=

  +jtfWordgetText()

  VarByte vb = DictFramegetPage(url)

  String content = new String(vbto_byte() utf

  String tagBegin = <acceptation>;

  String tagEnd = </acceptation>;

  StringBuilder sb = new StringBuilder()

  int cur = ;

  while((cur=contentindexOf(tagBegin cur))!=) {

  cur += ;

  int curEnd = contentindexOf(tagEnd cur)

  sbappend(contentsubstring(cur curEnd))

  curEnd += ;

  }

  if(sbtoString()length()==) sbappend(抱歉 未查詢到詞義!

  jtameansetText(sbtoString())

  AWTUtilitiessetWindowOpacity(jdmeanf)

  jdmeansetBounds(jdmeangetX() jdmeangetY()

  } catch (Exception ex) {

  jtameansetText;

  AWTUtilitiessetWindowOpacity(jdmeanf)

  jdmeansetBounds(jdmeangetX() jdmeangetY()

  }

  }

  }

  @Override

  public void keyPressed(KeyEvent arg) {

  }

  })

  }

  public static void _sleep(long m) {

  try {

  Threadsleep(m)

  } catch (InterruptedException e) {

  eprintStackTrace()

  }

  }

  public static VarByte getPage (String address) throws Exception {

  URL url = new URL(address)

  InputStream inStream = urlopenStream()

  VarByte page = new VarByte()

  int oneTimeSize = ;

  byte[] b = new byte[oneTimeSize];

  int len;

  while((len=inStreamread(b))!=) {

  if(len!=oneTimeSize) {

  b = pyOf(b len)

  }

  pageappend(b)

  }

  if(inStream!=null) inStreamclose()

  return page;

  }

  }

  class VarByte {

  byte[]  b = new byte[];

  int capacity = ;

  int len = ;

  public VarByte append(byte[] ba) {

  if(len+balength>capacity) {

  byte[] tmp = new byte[(len+balength)*];

  Systemarraycopy(b tmp len)

  Systemarraycopy(ba tmp len balength)

  b = tmp;

  len = len+balength;

  capacity = len*;

  } else {

  Systemarraycopy(ba b len balength)

  len = len + balength;

  }

  return this;

  }

  public byte[] to_byte() {

  byte[] btmp = new byte[len];

  Systemarraycopy(b btmp len)

  return btmp;

  }

  public byte[] to_byte(int start int len) {

  byte[] btmp = new byte[len];

  for(int i=; i<len; i++) {

  btmp[i] = b[start+i];

  }

  return btmp;

  }

  public int size() {

  return len;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25904.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.