package cn.com.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter; import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder; public class testDrap {
static int xx , yy;
static Point p = new Point(0, 0);
static int w,h;
static boolean b = true;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBorder(new LineBorder(Color.black));
panel.setPreferredSize(new Dimension(260,55)); frame.setBounds(100, 100, 450, 300);
final JLabel label = new JLabel(){
public void repaint() {
this.setLocation(p);
this.setSize(w,h);
super.repaint();
}
};
LineBorder border = new LineBorder(Color.black);
label.setBorder(border);
label.setForeground(Color.black);
label.setPreferredSize(new Dimension(50,30));
label.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
b = false;
}
public void mousePressed(MouseEvent mouseEvent) {
xx = mouseEvent.getX();
yy = mouseEvent.getY();
w = label.getSize().width;
h = label.getSize().height;
b = true;
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent e) {
if (label.getSize().width-8<=e.getX()) {
label.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));//右
} else if (8>=e.getX()) {
label.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));//左
} else {
label.setCursor(Cursor.getDefaultCursor());
}
}
public void mouseClicked(MouseEvent arg0) {
}
});
label.addMouseMotionListener(new MouseAdapter() { public void mouseMoved(MouseEvent e) {
Cursor cursor = label.getCursor();
if (label.getSize().width-5<=e.getX()) {
label.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));//右
} else if (5>=e.getX()) {
label.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));//左
} else {
label.setCursor(Cursor.getDefaultCursor());
}
if (b) {
label.setCursor(cursor);
}
};
public void mouseDragged(MouseEvent e) {
p = label.getLocation();
if (label.getCursor().getType() == Cursor.W_RESIZE_CURSOR) { Point point = label.getLocation();
p.x = e.getPoint().x + point.x - 0;
p.y = point.y;
label.setLocation(p); w = label.getSize().width - e.getPoint().x;
h = label.getSize().height;
label.setSize(w,h);
} else if (label.getCursor().getType() == Cursor.E_RESIZE_CURSOR) { w = e.getPoint().x;
h = label.getSize().height;
label.setSize(w,h); label.setLocation(p);
} else {
Point point = label.getLocation();
////////////随意移动
            //p.x = e.getPoint().x + point.x - xx;
            //p.y = e.getPoint().y + point.y - yy;
//label.setLocation(e.getPoint().x + point.x - xx,e.getPoint().y + point.y - yy);
            ////////////沿着X轴方向移动
p.x = e.getPoint().x + point.x - xx;
p.y = point.y;
label.setLocation(e.getPoint().x + point.x - xx,point.y); }
}
}
);
panel.add(label);
frame.add(panel);
JButton btn = new JButton("确认");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < panel.getComponentCount(); i++) {
Component c = panel.getComponent(i);
System.out.println(c.getLocation().x);
}
}
});
frame.add(btn);
frame.setVisible(true);
}
}

java实现控件的移动及使用鼠标改变控件大小的更多相关文章

  1. (转)C# WinForm中 获得当前鼠标所在控件 或 将窗体中鼠标所在控件名显示在窗体标题上

    原文地址:http://www.cnblogs.com/08shiyan/archive/2011/04/14/2015758.html /********************** * 课题:将窗 ...

  2. c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集

    界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ...

  3. WinForm开发-界面控件到实体,实体到界面控件自动绑定

    在WinForm开发中,我们是不是为绑定界面控件的数据而每个控件每个控件的赋值?在保存修改时是不是也是每个控件每个控件的赋值到实体中?字段一多,那简直就是噩梦.有没有像Web中那样方便的方法直接就自动 ...

  4. VC 使用OnCtlColor函数来改变控件颜色(引用)

    在MFC类库提供了CWnd::OnCtlColor函数,在工作框架的子窗口被重画时将调用该成员函数.因此可以重载WM_CTLCOLOR消息的响应函数.此函数的原型:afx_msg HBRUSH OnC ...

  5. WinForm------DockManager控件的使用方法(里面包含DockPanel控件)

    1.在"引用"中添加DevExpress.XtraBars和DexExpress.XtraNavBar程序集 2.往工具栏拖出DockManager控件,点击右上角的小三角,再点击 ...

  6. MFC中改变控件的大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  7. wxpython 中 用鼠标拖动控件 总结

    #encoding: utf-8 import wx import os import noname class Frame( noname.MyFrame1 ): def __init__(self ...

  8. Asp.Net 如何获取所有控件&如何获取指定类型的所有控件

    一. Asp.Net Page页面中访问所有控件的属性为: Page.Controls 控件的结构是树结构. 二.获取指定类型所有控件实例: 1.递归方法定义: private void GetCon ...

  9. 在WinForm中使用委托来在其他线程中改变控件的显示

    假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...

随机推荐

  1. 快速部署docker

    前言:docker就不用说了,好东西啊.更好的利用服务器的资源,各个服务是相互隔离的,文件的存放更加规律,也好清理空间及数据备份 docker安装-----社区版ce(免费的,另一版本收钱的) 系统: ...

  2. 饮冰三年-人工智能-Python-23 Python PyCharm 使用中常见的问题

    一:软件工具使用中遇到的问题 1:AttributeError: module 'pip' has no attribute 'main'问题 处理方法: a:找到JetBrains\PyCharm ...

  3. IIS 支持 m3u8

    加上俩 MIME 类型就可以了 <mimeMap fileExtension=".m3u8" mimeType="application/x-mpegURL&quo ...

  4. [转] 扩展微信小程序框架功能

    通过第三方 JavaScript 库,扩展微信小程序框架功能. 扩展微信小程序框架功能(1)——Promise ES6 对 Promise 有了原生的支持,但微信开发者工具更新版本(0.11.1122 ...

  5. 模块度Q

    模块度:也称模块化度量值,是目前常用的一种衡量网络社区结构强度的方法. 常用语衡量一个社区划分结果的优劣:一个理想化的社区划分应该对应着社区内部节点间相似度尽可能的高,同时社区外部节点间的相异度尽可能 ...

  6. zabbix4.0构建实录

    [Nginx] #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@centos ...

  7. Codeforces 840D Expected diameter of a tree 分块思想

    Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚 ...

  8. PHP unicode与普通字符串的相互转化

    unicode转字符串 方法一:json /** * unicode转字符串,通过json转化 * @param $str * @return string */ function unicode_d ...

  9. String hashcode的兴趣试玩

    今天突然看到Hashcode和equals,==比较时,一时兴起,想了解一下hashcode生成规则,为什么hashcode相同,无法说明对象相等,但用equals说明相同,却可以推出对象的hashc ...

  10. eclipse下启动tomcat访问网址报404错误

    问题: 解决步骤 首先检查一遍自己tomcat的相关配置,如果无误,则继续下面的操作 1.打开server视图,双击tomcat进入编辑页面 选择其中的第二个选项,并且修改Server path中的内 ...