首页 > 移动平台 > 详细

软件工程——移动的HelloWorld

时间:2015-04-25 21:07:05      阅读:294      评论:0      收藏:0      [点我收藏+]
package disiti;
   
  import java.awt.Color;
  import java.awt.Cursor;
  import java.awt.Font;
  import java.awt.Point;
  import java.awt.event.MouseEvent;
   
  import javax.swing.JLabel;
  import javax.swing.JWindow;
  import javax.swing.event.MouseInputListener;
   
  public class GuiHelloWorld extends JWindow {
  private static final long serialVersionUID = 1L;
  JLabel titleLbl;
  Font GuiHelloWorldFont;
   
  public GuiHelloWorld() {
  GuiHelloWorldFont = new Font("幼圆", Font.ITALIC, 28);
   
  this.getContentPane().setBackground(new Color(0x99FF66));
  this.setBounds(400, 200, 200, 60);
  this.setLayout(null);
   
  titleLbl = new JLabel(" Hello World!");
  titleLbl.setFont(GuiHelloWorldFont);
  titleLbl.setOpaque(true);
  titleLbl.setBackground(new Color(0x66CC00));
  titleLbl.setBounds(0, 0, 200, 60);
  this.add(titleLbl);
   
  // 鼠标事件处理类
  MouseEventListener mouseListener = new MouseEventListener(this);
  titleLbl.addMouseListener(mouseListener);
  titleLbl.addMouseMotionListener(mouseListener);
  this.setVisible(true);
  }
   
  public static void main(String[] args) {
  new GuiHelloWorld();
  }
  }
   
  class MouseEventListener implements MouseInputListener {
  Point origin; // 鼠标拖拽想要移动的目标组件
  GuiHelloWorld frame;
   
  public MouseEventListener(GuiHelloWorld frame) {
  this.frame = frame;
  origin = new Point();
  }
   
  public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
   
  }
   
  public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  origin.x = e.getX();
  origin.y = e.getY();
  }
   
  public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub
   
  }
   
  public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
  }
   
  public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  }
   
  public void mouseDragged(MouseEvent e) {
  // TODO Auto-generated method stub
  Point p = this.frame.getLocation();
  this.frame.setLocation(p.x + (e.getX() - origin.x), p.y
  + (e.getY() - origin.y));
  }
   
  public void mouseMoved(MouseEvent e) {
  // TODO Auto-generated method stub
   
  }
   
  }

软件工程——移动的HelloWorld

原文:http://www.cnblogs.com/tibosi/p/4456538.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!