软件工程——移动的HelloWorld

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
  
 }
  
 }

转载于:https://www.cnblogs.com/tibosi/p/4456538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值